Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kongondo/5728082 to your computer and use it in GitHub Desktop.
Save kongondo/5728082 to your computer and use it in GitHub Desktop.
add page field as property to page example
<?php
/**
* ProcessWire AddPageFieldProperty demonstration module
*
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class AddPageFieldProperty 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(
// The module'ss title, typically a little more descriptive than the class name
'title' => 'AddPageFieldProperty',
// version: major, minor, revision, i.e. 100 = 1.0.0
'version' => 101,
// summary is brief description of what this module is
'summary' => 'An example module.',
'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() {
// exclude hook from admin urls
if(isset($_GET['it']) && strpos($_GET['it'], ltrim($this->config->urls->admin,"/")) !== false) return;
// add a hook before rendering a page to modify it before sent to template output
$this->addHookBefore('Page::render', $this, 'addProperty');
}
/**
* add property to a certain page to add selected "option name"
*
* if($page->somepagefield->optionname) { ... }
* where "optionname" is the name of the selected page in the page field "somepagefield"
*
* @param HookEvent $event event object
*/
public function addProperty(HookEvent $event){
$page = $event->object;
//if($page->template != "basic-page") return;
// check for if a page field is set and is not empty
if(count($page->somepagefield)){
foreach($page->somepagefield as $l){
$page->set("$l->name",true); // set property to page temporarely
}
}
// add more other checks to your needs
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment