Skip to content

Instantly share code, notes, and snippets.

@somatonic
Created July 16, 2012 11:17
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 somatonic/3122191 to your computer and use it in GitHub Desktop.
Save somatonic/3122191 to your computer and use it in GitHub Desktop.
FieldHelper.module
<?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