Skip to content

Instantly share code, notes, and snippets.

@outflux3
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save outflux3/3e76a1338b61d708157c to your computer and use it in GitHub Desktop.
Save outflux3/3e76a1338b61d708157c to your computer and use it in GitHub Desktop.
Page Docs Tab for Processwire
<?php
/**
* ProcessWire Page Docs Tab
*
* A little addon to display help docs relevant to the page being edited.
*
* @copyright Copyright (c) 2015, Macrura
*
*/
class PageDocsTab extends WireData implements Module, ConfigurableModule {
/**
* @var Page Page being edited
*
*/
private $editedPage;
/**
* Return information about this module (required)
*
* @return array
*
*/
static public function getModuleInfo() {
return array(
'title' => 'Page Docs Tab',
'summary' => 'Adds a tab with related documentation.',
'version' => 100,
'author' => 'Macrura, based work by Niklas Lakanen',
'autoload' => true
);
}
/**
* __construct() is the right place to init config vars before they'll get populated
*
*/
public function __construct() {
$this->enabledTemplates = array();
}
/**
* init() is called when PW's API is ready
*
*/
public function init() {
}
/**
* ready() is called when both PW's API is ready and $page object is initialized (for autoload modules!)
* Add the hook here to be able to skip it based on the template of the edited page.
*
*/
public function ready() {
// we're interested in page editor only
if(wire('page')->process != 'ProcessPageEdit') return;
// skip changing templates (only target the actual edit form)
$id = (int)$this->input->get('id');
if(!$id) return;
// wire('page') would be the page with ProcessPageEdit
// GET parameter id tells the page that's being edited
$this->editedPage = wire('pages')->get($id);
// don't even consider system templates
if($this->editedPage->template->flags & Template::flagSystem) return;
// hook only if
// 1) no templates have been chosen (=all enabled) OR
// 2) the template of the edited page has been chosen
if(count($this->enabledTemplates) == 0 or
in_array($this->editedPage->template->name, $this->enabledTemplates)) {
$this->addHookAfter('ProcessPageEdit::buildForm', $this, 'hookBuildForm');
}
}
/**
* Add Docs tab to page edit form
*
*/
public function hookBuildForm(HookEvent $event) {
// get the InputFieldForm object from the event (return value of buildForm())
$form = $event->return;
// create the tab
$docTab = new InputfieldWrapper();
$docTab->attr('id', $this->className() . 'Docs');
$docTab->attr('title', $this->_('Docs'));
$field = $this->modules->get("InputfieldMarkup");
// change this selector based on how you have things setup...
$docs = wire('pages')->find("template=doc-main|doc-child, template_select={$this->editedPage->template->id}");
if(count($docs)) {
$body = '';
foreach($docs as $doc) {
$body .= $doc->body;
}
$field->markupText = $body;
} else {
$field->markupText = "No applicable docs found";
}
// append the markup to the tab and the tab to the form
$docTab->append($field);
$form->append($docTab);
}
/**
* Return an InputfieldsWrapper of Inputfields used to configure the class
*
* @param array $data Array of config values indexed by field name
* @return InputfieldsWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
$wrapper = new InputFieldWrapper();
$fieldEnabledTemplates = wire('modules')->get('InputfieldAsmSelect');
$fieldEnabledTemplates->attr('name+id', 'enabledTemplates');
$fieldEnabledTemplates->label = __('Enabled templates', __FILE__);
$fieldEnabledTemplates->description = __('"Docs" tab will only be shown for chosen templates. If no template is chosen, "Docs" tab will be shown for all templates.', __FILE__);
$fieldEnabledTemplates->attr('title', __('Enable template', __FILE__));
$fieldEnabledTemplates->setAsmSelectOption('sortable', false);
// populate with all available templates
foreach(wire('templates') as $t) {
// filter out system templates
if(!($t->flags & Template::flagSystem)) $fieldEnabledTemplates->addOption($t->name);
}
if(isset($data['enabledTemplates'])) $fieldEnabledTemplates->value = $data['enabledTemplates'];
$wrapper->add($fieldEnabledTemplates);
return $wrapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment