ProcessWire Module Generator

Press space to see suggestions.
Press space to see suggestions.
Press space to see suggestions.
Press space to see suggestions.
Press space to see suggestions.

FAQ

What is "autoload"?

If true, this is considered an "autoload" module and ProcessWire will automatically load the module during its bootstrap process. This is typically used for modules that attach runtime hooks.

What is "singular" and which modules should activate it?

If true, ProcessWire will only instantiate 1 instance of the module and use that instance every time the module is called.
Autoload modules and modules that attach hooks are usually singular.
Modules that may have multiple instances (like Inputfields) should NOT be singular.

What is "requires"?

A list of dependencies: other module class names required by this module. If just one module is required, then it can also be a string with just the module class name.

What is "installs"?

A list of module class names that this module will handle the install and uninstall for. If your module does not install/uninstall them, ProcessWire will automatically install/uninstall them immediately after your module. Like the 'requires' property, this may be a string if there's only one.

What is "permanent"?

If true, the module may not be uninstalled once installed. This is intended for some of ProcessWire's core modules and this probably is best left omitted from your own modules.

Can I use line-breaks in the summary?

No. They get replaced by a whitespace.

How to use Hooks and which Hooks can I use?

You can hook into every function of every module (like "ProcessPageEdit.module") which starts with three underscores (e.g. "___execute()"). So your hook reference would look like this: "ProcessPageEdit::execute".
Learn more about how to use hooks in the offical documentation: https://processwire.com/api/hooks/

Find a list of all possible hooks here: http://processwire.com/api/hooks/captain-hook/

Best practice

How to create a page while installation with a non-process module?

Here is a short example on how you could do it (of course there are a lot of different ways to archive this):

public function ___install() {
	// Creates a new Page object
	$page = new Page();
	
	// use your own selector to choose the parent page
	$parent = $this->pages->get("name=setup");
	$page->parent = $parent;
	
	// choose a template
	$page->template = $this->templates->get('admin');
	
	// name and title
	$page->name = "your-page";
	$page->title = "Your Page";
	
	// .. and save the new page object as a real page
	$page->save();

	// success message
	$this->message(sprintf($this->_("Installed to %s"), $page->path));
}

public function ___uninstall() {
	$parent = $this->pages->get("name=setup");
	
	// the name has to be the same as defined above
	// you could use a constant variable for this
	$page = $parent->child("name=your-page");
	
	// only delete page if existing
	if($page->id) {
		$this->pages->delete($page);
		$this->message(sprintf($this->_("Removed %s"), $page->path));
	}
}
How to store data?

To store data you have to implement "ConfigurableModule" even if you don't need a config page for your module. Just choose it in the generator above.

Here are two functions you could use in your module:

private function getConfigData() {
	// fetch all of the default config data...
	$configData = $this->modules->getModuleConfigData($this);
	
	// ... and merge it with already saved values
	$data = array_merge(self::getDefaultConfig(), $configData);
	
	return $data;
}

private function setConfigData(array $newData) {
	// get current data
	$data = $this->getConfigData();
	
	// replace with new data if changed and keep old data otherwise
	$data = array_merge($data, $newData);
	
	// save config data
	wire('modules')->saveModuleConfigData($this, $data);
	
	return $data; // or return true or whatever you like
}
How to add/modify the breadcrumb navigation and the headline?

Add the following line at the top of your "___executeSubfunction" function:

// add breadcrumb
$this->fuel('breadcrumbs')->add(new Breadcrumb("./your-link/", "Page Title")); 

// Add headline
Wire::setFuel('processHeadline', 'Page Title'); 
How to add a table to your "Process" module?

ProcessWire offers you an easy way to do this: AdminDataTable. You can use it as in the following example:

// create a new form if not already existing
$form = $this->modules->get("InputfieldForm");

// create a new Inputfield Markup because MarkupAdminDataTable
// is not an inputfield but just returns rendered markup code
// which we're going to use to poulate the inputfield
$field = $this->modules->get('InputfieldMarkup');

// Btw: You could use MarkupAdminDataTable wthout an Inputfield, too
$table = $this->modules->get('MarkupAdminDataTable');
$table->setEncodeEntities(false);

// define header labels and add them to the table
$header = array(
	'ID',
	'City',
	'Name'
);
$table->headerRow($header);

// define body data
$body = array(
	array(
		1,
		'Berlin',
		'Lorem Ipsum'
	),
	array(
		2,
		'Tokyo',
		'Foo Bar'
	),

	array(
		3,
		'Seoul',
		'Kim Something'
	)
);


// ... and add them to table row for row
foreach ($body as $item) {
	$table->row($item);
}

// render the table to get the markup
$markup = $table->render();

// Now we could add this markup to the Inputfield or do something else with it
$field->attr('value', $markup);

// add the field to the form and render the form (optional)
$form->add($field);
return $form->render();
How to insure needed modules are loaded?

To activate modules you can just use the following command. (preferable in your init function)

wire('modules')->get('NeededModule');
Whre can I find more code samples?

The site processwire-recipes.com offers you a great ressource for finding more code samples and best practices.