Skip to content

Instantly share code, notes, and snippets.

@somatonic
Last active August 29, 2015 14:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save somatonic/c9db5c43f10d050bb14d to your computer and use it in GitHub Desktop.
Example InptfieldPageTable hook module to remove "Add New" button on a condition
<?php
/**
* Example Hooks to hack InputfieldPageTable to not render add buttons on a condition
* This will remove "add new" buttons if there's more than 2 entries
*
* - First addHookBefore InputfieldPageTable::render to count the value (table rows)
* - If value is greater than 1, we hadd another hook to InputfieldButton::render (used by page table for the buttons)
* and overwrite it with an empty string.
* - Add another hook after page table render to remove the button hooks, to not remove any other buttons rendered
* after this page table
*
* @author Soma <philipp@urlich.ch>
*
*
*/
class MyPageTableHooks extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'My PageTable Hooks',
'version' => 1,
'summary' => 'An example module to hide "Add New" buttons on a condition.',
'href' => 'http://www.processwire.com',
'singular' => true,
'autoload' => "process=ProcessPageEdit",
'icon' => 'smile-o',
);
}
/**
* 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() {
$this->buttonHook = null;
$this->addHookBefore("InputfieldPageTable::render", $this, "renderPageTable");
$this->addHookAfter("InputfieldPageTable::render", $this, "renderPageTableAfter");
}
public function renderPageTable(HookEvent $event){
// get the table field
$table = $event->object;
// make sure this is our field
if($table->name !== "mypagetable") return;
// if there's 2+ rows add another hook to remove returned markup string
// rendered by InputfieldButton::render
if(count($table->attr("value")) > 1) {
$this->buttonHook = $this->addHookAfter("InputfieldButton::render", null, function(HookEvent $event){
// overwrite/remove button markup
$event->return = '';
});
}
}
public function renderPageTableAfter(HookEvent $event){
// if there's any button hooks set, remove the hook to not remove all buttons added to page
// rendered after this page table
if($this->buttonHook) $this->removeHook($this->buttonHook);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment