Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.