Last active
August 29, 2015 13:56
-
-
Save kongondo/8890310 to your computer and use it in GitHub Desktop.
Example Configurable ProcessWire Process Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Example by Ryan Cramer @ProcessWire - http://processwire.com/talk/topic/5480-configurable-process-module-need-walk-through/?p=53729 | |
class YourModule extends WireData implements Module, ConfigurableModule { | |
public static function getModuleInfo() { | |
return array('title' => 'Your Module', 'version' => 1); | |
} | |
const defaultValue = 'smith'; | |
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 'smith' 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 | |
*/ | |
/*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); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment