Created
July 16, 2012 11:17
-
-
Save somatonic/3122191 to your computer and use it in GitHub Desktop.
FieldHelper.module
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 Module FieldHelper | |
* | |
* | |
* 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 FieldHelper 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' => 'FieldHelper', | |
'version' => 100, | |
'summary' => 'Hide fields depending on permission, role, or user.', | |
'singular' => true, | |
'autoload' => true | |
); | |
} | |
public function init() { | |
// add after-hook to the inputfield render method | |
$this->addHookAfter("Inputfield::render", $this, "renderField"); | |
} | |
/** | |
* Hide fields depending on permission or possibly role, user | |
* | |
*/ | |
public function renderField(HookEvent $event) { | |
// get the current field | |
$field = $event->object; | |
// example 1 | |
if($field->name == "pre_content") { | |
if(!$this->user->hasPermission("admin-fields")){ | |
$event->return = ''; | |
} | |
} | |
// example 2 | |
if($field->name == "after_content") { | |
if(!$this->user->hasRole('superuser')){ | |
$event->return = ''; | |
} | |
} | |
// example 3 | |
if($field->name == "page_scripts") { | |
if(!$this->user->name('admin')){ | |
$event->return = ''; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment