Jump to content

Configurable Process Module (Need walk through)


renobird
 Share

Recommended Posts

I'm trying to get my head around how to make a configurable Process Module.

I've read the this tutorial, but I could use a little more hand holding.

Let's say I have:

static public function getModuleConfigInputfields(array $data) {

        $fields = new InputfieldWrapper();
        $modules = Wire::getFuel('modules');

        $field = $modules->get("InputfieldText");
        $field->attr('name', 'defaultSelector');
        $field->label = __("Default Selector", __FILE__);
        $field->description = __("Specify a default Selector", __FILE__);
        $fields->append($field);

        return $fields;
    }

How do I set/get the values for $fields?

Once set, how do I access the values in $fields elsewhere in the module.

Anyone mind posting a very basic example?

Link to comment
Share on other sites

As you wait for other responses, have a look at the following examples from AdminSaveActions module. It looks really straightforward. Thanks for asking this question...I'll be needing this soon :-). Seems, you set the defaults in one function, then build the form/fields in another function using the defaults previously set...or something along those lines :-)

AdminSaveActions module

static public function getDefaultConfig() {}

https://github.com/niklaka/AdminSaveActions/blob/master/AdminSaveActions.module#L51

public function __construct() {}

https://github.com/niklaka/AdminSaveActions/blob/master/AdminSaveActions.module#L105

public static function getModuleConfigInputfields(array $data) {}

https://github.com/niklaka/AdminSaveActions/blob/master/AdminSaveActions.module#L390

MaintenanceMode module

static public function getDefaultData() {}

https://github.com/Notanotherdotcom/Maintenance-Mode/blob/master/MaintenanceMode.module#L25

public function __construct() {}

https://github.com/Notanotherdotcom/Maintenance-Mode/blob/master/MaintenanceMode.module#L38

static public function getModuleConfigInputfields(array $data) {}

https://github.com/Notanotherdotcom/Maintenance-Mode/blob/master/MaintenanceMode.module#L52

ProcessTrashman - Last one, 

static public function getDefaultData() {}

https://github.com/apeisa/Trashman/blob/master/ProcessTrashman.module#L42

public function __construct() {}

https://github.com/apeisa/Trashman/blob/master/ProcessTrashman.module#L54

static public function getModuleConfigInputfields(array $data) {}

https://github.com/apeisa/Trashman/blob/master/ProcessTrashman.module#L255

MarkupRSS module -  Sorry, the very last one (this one has more detailed defaults and some notes)

protected static $defaultConfigData = array()

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Markup/MarkupRSS.module#L32

public function __construct() {}

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Markup/MarkupRSS.module#L67

static public function getModuleConfigInputfields(array $data) {}

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Markup/MarkupRSS.module#L219

Tutorial on forum - Create your first Module with configuration settings and a overview page:

http://processwire.com/talk/topic/2318-create-your-first-module-with-configuration-settings-and-a-overview-page/

However, this Tut does not set defaults, which I think I have read somewhere is good practice to do?

OK, sorry, am overdoing it but this will help me later too ;-)

Edit: corrected wrong links + added a tutorial from the forums

Edit 2: Something has been eating my links! Anyway, added methods/properties names...

Edited by kongondo
  • Like 8
Link to comment
Share on other sites

Hey reno,

You can set your defaults like this. Just put it before your init():

    protected static $configDefaults = array(
        "defaultSelector" => "default value here"
    );

    /**
     * Data as used by the get/set functions
     *
     */
    protected $data = array();

Add this line to your inputfield:

$field->attr('value', $data["defaultSelector"]);

And add the following at the top of your static public function getModuleConfigInputfields(array $data) {:

foreach(self::$configDefaults as $key => $value) {
     if(!isset($data[$key]) || $data[$key]=='') $data[$key] = $value;
}

You can then access this throughout your module using:

$this->defaultSelector;

At least that is how I have been doing it - maybe someone else has something cleaner :)

Does that make sense?

PS, Sorry for all the consecutive edits. I really shouldn't try to offer device when I am busy with other things and rushing :)

EDIT Again - I just read that tutorial you pointed to and I see if uses: public function __construct() to set the default values. I haven't used that before. I guess I started by copying from other modules and never came across that approach. I guess there are a few different options, but construct does seem cleaner.

Edited by adrian
  • Like 11
Link to comment
Share on other sites

And add the following at the top of your static public function getModuleConfigInputfields(array $data) {:

foreach(self::$configDefaults as $key => $value) {
    if(!isset($data[$key]) || $data[$key]=='') $data[$key] = $value;
}
 

 

Be careful here, only keys that are not set in the $data array should inherit the default value. Otherwise if a config value is empty, you'd overwrite it with the default value all the time, if the default value ist not empty.

Suppose a checkbox with possible values 1 and ''

  1. default=1 (checked)
  2. User opens Module Config and does uncheck the box
  3. After reloading, the box would be displayed as checked

Setting the default data in the constructor makes sure that you'll have every config key available as property (->key). The workflow here is:

  1. Pw creates your module --> calls constructor
  2. Pw does set all the stored config values to the module
  3. Pw calls init() method

As you can see, modified default config values will get overridden in step 2.

Default config values that are not yet stored in the database are set by you in the constructor.

  • Like 5
Link to comment
Share on other sites

EDIT Again - I just read that tutorial you pointed to and I see if uses: public function __construct() to set the default values. I haven't used that before. I guess I started by copying from other modules and never came across that approach. I guess there are a few different options, but construct does seem cleaner.

@Adrian: Yes you are right, it does. Btw, all the modules I pointed to also use the __construct() method. Wanze explains it well above why this is important. Thanks Wanze! Thanks Adrian for the write-up!

I have edited my first post with the names of the methods/properties. My links kept being mangled up and anyway, when code is updated, they may move lines ...

Link to comment
Share on other sites

I think these guys covered it really well, but here's an overly simple alternate example in case it helps. 

class YourModule extends WireData implements Module, ConfigurableModule {

  public static function getModuleInfo() {
    return array('title' => 'Your Module', 'version' => 1); 
  }

  const defaultValue = 'renobird';

  public function __construct() {
    $this->set('yourname', self::defaultValue); // set default value in construct
  }

  public function init() {
    // while you need this function here, you don't have to do anything with it
    // note that $this->yourname will already be populated with the configured
    // value (if different from default) and ready to use if you want it
  }

  public function execute() {
    // will return configured value, or 'renobird' if not yet configured
    return $this->yourname; 
  }

  public static function getModuleConfigInputfields(array $data) {
    // if yourname isn't yet in $data, put our default value in there
    if(!isset($data['yourname'])) $data['yourname'] = self::defaultValue;

    $form = new InputfieldWrapper();
    $f = wire('modules')->get('InputfieldText');
    $f->name = 'yourname'; 
    $f->label = 'Enter your name'; 
    $f->value = $data['yourname'];
    $form->add($f);

    return $form; 
  }
}
 

If you have the need to manage several configuration values, then you can save yourself some time by using a static array of default values like in the examples before mine. 

Also this: 

foreach(self::$configDefaults as $key => $value) {
     if(!isset($data[$key]) || $data[$key]=='') $data[$key] = $value;
}

could also be written as this:

$data = array_merge($data, self::$configDefaults); 
  • Like 6
Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...