Skip to content

Instantly share code, notes, and snippets.

@arjenblokzijl
Forked from somatonic/PageListActionHook.module
Last active August 29, 2015 14:07
Show Gist options
  • Save arjenblokzijl/7d4193aec2b89ed88485 to your computer and use it in GitHub Desktop.
Save arjenblokzijl/7d4193aec2b89ed88485 to your computer and use it in GitHub Desktop.
<?php
/**
* ProcessWire example demonstration module
*
* PageListActionHook autoload module once installed will remove "new" action from second level pages
* using the template "basic-page"
*
*/
class PageListActionHook extends WireData implements Module, ConfigurableModule {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'PageListActionHook',
'version' => 1,
'summary' => 'An example module on how to hook into Page List Actions.',
'href' => 'http://www.processwire.com',
'singular' => true,
'autoload' => true,
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// important here is to hook into ProcessPageListRender and not ProcessPageList
$this->addHookAfter("ProcessPageListRender::getPageActions", $this, "hookPageActions");
}
public function hookPageActions($event) {
// get the argument named
$page = $event->arguments("page");
// only for pages that use the basic-page template
if($page->template == "basic-page") {
// only allow 2 levels of basic-page
// we count the parents of the current child
if($page->parents->count() > 1){
// catch the actions array returned by the hooked method
// ProcessPageListRender::getPageActions
$actions = $event->return;
// find new action to remove it from array
foreach($actions as $key => $action){
if($action['cn'] == 'New') {
// remove action from actions array
array_splice($actions, $key, 1);
}
}
// send back the modified $actions array back to the event object
$event->return = $actions;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment