Last active
August 29, 2015 13:57
-
-
Save somatonic/9494427 to your computer and use it in GitHub Desktop.
Example module to modify/remove page "addable" permission for pages with level greater than one and basic-page template
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 | |
/** | |
* ProcessWire example demonstration module | |
* | |
* Page::addable hook module, once installed will set permission "addable" to false | |
* for pages using basic-page template and with level greater than 1 | |
*/ | |
class PageAddableHook 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' => 'PageAddableHook', | |
'version' => 1, | |
'summary' => 'An example module on how to hook into Page::addable page permission hook.', | |
'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() { | |
// hook into page permissions | |
// note: Page::addable is also added via hook in wire/modules/PagePermissions.module | |
// that's why you won't find those in wire/core/Page.php | |
$this->addHookAfter("Page::addable", $this, "hookPageAddable"); | |
} | |
public function hookPageAddable($event){ | |
// already don't have add childs permission so we don't need to do anything | |
if(!$event->return) return; | |
// get the page, the class in this case is the Page | |
$page = $event->object; | |
if($page->template == "basic-page"){ | |
// only allow 2 levels of basic-page | |
if($page->parents->count() > 1){ | |
// return false to remove "addable" permission | |
$event->return = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment