Process Module (ProcessWire)

Aus Wikizone
Version vom 17. Dezember 2021, 13:27 Uhr von 134.3.241.3 (Diskussion) (→‎Select Feld)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Links[Bearbeiten]

ProcessWire - Module schreiben
https://github.com/ryancramerdesign/ProcessHello/blob/master/ProcessHello.info.php - Beispiel Process Modul
https://processwire.com/blog/posts/building-custom-admin-pages-with-process-modules/ - Gutes Tutorial von Bernhard Baumrock
https://processwire.com/talk/topic/16185-how-to-build-a-simple-dashboard-with-ajax-functionality-using-a-process-module/

Basics[Bearbeiten]

  • Process Module erweitern die Funktionalität im Admin Bereich.
  • In der Regel erstellt das Modul eine Seite im Admin Zweig des Seitenbaums. So kann man über das Menü zugreifen.


Seitenausgabe mit execute()[Bearbeiten]

  • eine execute (oder ___execute mit Hook) Funktion gibt den Output der Seite
  • Output für Unterseiten erzeugt man mit executeUnterseite (ergibt dann z.B. setup/meinModul/Unterseite
executeMySecondPage translates to my-second-page in the URL
executeMysecondpage translates to mysecondpage in the URL

Admin Bereich anpassen[Bearbeiten]

Vorsicht mit dem Namespace. Wenn er sich in den Dateien unterscheidet, verarbeitet PW die config Dateien nicht richtig. Bei mir hat es ohne Namespace in allen Dateien funktioniert.

Seiten im Admin Hinzufügen[Bearbeiten]

Geht über die getModuleInfo Funktion oder über die MeinModul.Info.php Konfigurationsdatei (siehe Beispiel Modul)

// page that you want created to execute this module
	'page' => array(
		'name' => 'helloworld',
		'parent' => 'setup', 
		'title' => 'Hello World'
	),

Extra Navigation im Admin Hinzufügen[Bearbeiten]

Wie bei zusätzlicher Seite

	// optional extra navigation that appears in admin
	// if you change this, you'll need to a Modules > Refresh to see changes
	'nav' => array(
		array(
			'url' => '', 
			'label' => 'Hello', 
			'icon' => 'smile-o', 
		), 
		array(
			'url' => 'something/', 
			'label' => 'Something', 
			'icon' => 'beer', 
		),
	)

JSON Nav[Bearbeiten]

Man kann auch die Option

'useNavJSON' => true,

setzen, dann kann man über die Funktion

public function ___executeNavJSON(array $options = array())

aufwändigere Dinge realisieren (Beispiel in Adrians ProcessAdminActions)

Zugriff regeln[Bearbeiten]

Ebenso lassen sich Permissions definieren

	// name of permission required of users to execute this Process (optional)
	'permission' => 'helloworld', 
	// permissions that you want automatically installed/uninstalled with this module (name => description)
	'permissions' => array(
		'helloworld' => 'Run the HelloWorld module'
	), 
	

Modul Konfiguration hinzufügen[Bearbeiten]

Seit 2.5.5 über eine .config.php Datei möglich. Die Datei lautet wie die Moduldatei aber mit angehängtem .config und die Klasse darin erweitert die Klasse ModuleConfig.

https://processwire.com/blog/posts/new-module-configuration-options/#enter-the-new-moduleconfig-class

Beispiel mit Funktionen

class TestConfig extends ModuleConfig {
  public function getDefaults() {
    return array(
      'fullname' => '',
      'color' => 'blue',
      'age' => 40,
    );
  }
  public function getInputfields() {
    $inputfields = parent::getInputfields();

    $f = $this->modules->get('InputfieldText');
    $f->attr('name', 'fullname');
    $f->label = 'Full Name';
    $f->required = true;
    $inputfields->add($f);

    $f = $this->modules->get('InputfieldSelect');
    $f->attr('name', 'color');
    $f->label = 'Favorite Color';
    $f->options = array(
      'red' => 'Red',
      'green' => 'Green',
      'blue' => 'Blue'
    );
    $inputfields->add($f);

    $f = $this->modules->get('InputfieldInteger');
    $f->attr('name', 'age');
    $f->label = 'Your Age';
    $inputfields->add($f);

    return $inputfields;
  }
}

Beispiel aus dem ProcessHello Modul mit assoziativem Array

ProcessHello.config.php

<?php

/**
 * Configure the Hello World module
 *
 * This type of configuration method requires ProcessWire 2.5.5 or newer.
 * For backwards compatibility with older versions of PW, you'll want to
 * instead want to look into the getModuleConfigInputfields() method, which
 * is specified with the .module file. So we are assuming you only need to
 * support PW 2.5.5 or newer here. 
 *
 * For more about configuration methods, see here: 
 * http://processwire.com/blog/posts/new-module-configuration-options/
 *
 * 
 */

class ProcessHelloConfig extends ModuleConfig {

	public function __construct() {

		$this->add(array(

			// Text field: greeting
			array(
				'name' => 'greeting', // name of field
				'type' => 'text', // type of field (any Inputfield module name)
				'label' => $this->_('Hello Greeting'), // field label
				'description' => $this->_('What would you like to say to people using this module?'), 
				'required' => true, 
				'value' => $this->_('A very happy hello world to you.'), // default value
			),

			// Radio buttons: greetingType
			array(
				'name' => 'greetingType', 
				'type' => 'radios', 
				'label' => $this->_('Greeting Type'), 
				'options' => array(
					// options array of value => label
					'message' => $this->_('Message'), 
					'warning' => $this->_('Warning'),
					'error' => $this->_('Error'), 
					),
				'value' => 'warning', // default value
				'optionColumns' => 1, // make options display on one line
				'notes' => $this->_('Choose wisely'), // like description but appears under field
			)
		)); 
	}
}

Beispiele[Bearbeiten]

Select Feld[Bearbeiten]

$f = $this->modules->get('InputfieldSelect');
$f->attr('name', 'color');
$f->label = 'Favorite Color';
$f->options = array(
  'red' => 'Red',
  'green' => 'Green',
  'blue' => 'Blue'
);
$inputfields->add($f);

Oder mit addOption()

// usage with all arguments
$inputfieldSelect->addOption(string $value, $label = null, array $attributes = null);
  $f = wire()->modules->get('InputfieldSelect');
  $options = array();
  foreach($categories as $cat){
    $f->addOption($cat->name, $cat->title);
  }
  $f->name = 'category';
  $f->label = 'Kategorie';
  $col2->add($f);

Checkboxen[Bearbeiten]

foreach($tags as $tag){
  $f = wire()->modules->get('InputfieldCheckbox');
  $f->name = $tag->name;
  $f->label = ' '; // do not show top label
  $f->label2 = $tag->title; // show side label
  $f->checkboxOnly = false; // you could hide label completely
  $f->class = 'uk-checkbox';
  $col1->add($f);
}

Starter mit Formular[Bearbeiten]

<?php

/**
 * ProcessWire EasyFirma tools
 * by Stephan Schlegel
 *
 *
 * Copyright (C) 2020 by Stephan Schlegel
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 */

class ProcessEasyFirma extends Process implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => __('Easy Firma Tools'),
            'version' => '0.0.1',
            'summary' => __('Tools to handle easyFirma v1 data imports'),
            'author' => 'Stephan Schlegel',
            'singular' => true,
            'autoload' => false,
            'icon'     => 'cog',
            'page' => array(
                'name' => 'easy-firma-tools',
                'parent' => "setup",
                'title' => "Easy Firma Tools"
            )
        );
    }


    public function init() {
        //parent::init();
        //$this->wire('config')->scripts->add($this->wire('config')->urls->{$this->className} . 'pluralize.js');
    }


    /**
     * Executed when root url for module is accessed
     *
     */
    public function ___execute() {
      // build a form
      $form = $this->buildForm1();

      // if user already sent data process form and returned processed output
      if($this->wire('input')->post->submit) {
          return $this->processForm1($form);
      }
      // else just render the form
      else {
          return $form->render();
      }

    }


    /**
     * Build the form
     *
     */
    protected function buildForm1() {

        $form = $this->wire('modules')->get("InputfieldForm");
        $form->method = 'post';

        $fieldset = $this->wire('modules')->get("InputfieldFieldset");
        $fieldset->label = __('Field settings', __FILE__);
        $form->add($fieldset);

        $f = $this->wire('modules')->get("InputfieldRadios");
        $f->name = 'action';
        $f->label = __('Action:', __FILE__);
        $f->description = __('What to do?', __FILE__);
        $f->required = true;
        $f->requiredAttr = true;
        $f->addOption('0', sprintf(__('Action 1', __FILE__), '&nbsp;'));
        $f->addOption('1', sprintf(__('Action 2', __FILE__), '&nbsp;'));
        $f->addOption('2', sprintf(__('Action 3', __FILE__), '&nbsp;', '&nbsp;'));
        $fieldset->add($f);

        $f = $this->wire('modules')->get("InputfieldSubmit");
        $f->name = 'submit';
        $f->value = __('Start Action', __FILE__);
        $form->add($f);

        return $form;
    }


    /**
     * Process the form and populate session variables with the results
     *
     */
    protected function processForm1(InputfieldForm $form) {

        if(count($form->getErrors()) ) {
            $this->error("Missing required field(s)");
            return $form->render();
        }
        var_dump($this->wire('input')->post->action);
        return("processing form");

    }
}

ProcessHello[Bearbeiten]

Beispiel

<?php

/**
 * ProcessHello.info.php
 * 
 * Return information about this module.
 *
 * If preferred, you can use a getModuleInfo() method in your module file, 
 * or you can use a ModuleName.info.json file (if you prefer JSON definition). 
 *
 */

$info = array(

	// Your module's title
	'title' => 'Hello: Process Module Example', 

	// A 1 sentence description of what your module does
	'summary' => 'A starting point module skeleton from which to build your own Process module.', 

	// Module version number: use 1 for 0.0.1 or 100 for 1.0.0, and so on
	'version' => 1, 

	// Name of person who created this module (change to your name)
	'author' => 'Ryan Cramer', 

	// Icon to accompany this module (optional), uses font-awesome icon names, minus the "fa-" part
	'icon' => 'thumbs-up', 

	// URL to more info: change to your full modules.processwire.com URL (if available), or something else if you prefer
	'href' => 'http://modules.processwire.com/', 

	// name of permission required of users to execute this Process (optional)
	'permission' => 'helloworld', 

	// permissions that you want automatically installed/uninstalled with this module (name => description)
	'permissions' => array(
		'helloworld' => 'Run the HelloWorld module'
	), 
	
	// page that you want created to execute this module
	'page' => array(
		'name' => 'helloworld',
		'parent' => 'setup', 
		'title' => 'Hello World'
	),

	// optional extra navigation that appears in admin
	// if you change this, you'll need to a Modules > Refresh to see changes
	'nav' => array(
		array(
			'url' => '', 
			'label' => 'Hello', 
			'icon' => 'smile-o', 
		), 
		array(
			'url' => 'something/', 
			'label' => 'Something', 
			'icon' => 'beer', 
		),
	)

	// for more options that you may specify here, see the file: /wire/core/Process.php
	// and the file: /wire/core/Module.php

);