Jump to content

Hook to hide inputfield in Admin


Alfred
 Share

Recommended Posts

Hello,

I'm trying to hide a specific field with a hook.

This is what I tried so far. Changing the field label works but the hiding  not.
Changing addHookBefore to addHookAfter makes no difference. 

I searched the forum and found similar thread but could not translate the answer to this situation.

Any help would be appreciated.

class HideFields extends WireData implements Module {
	
	public static function getModuleInfo() {
		
        return array(
                'title' => 'Hide Fields',
                'version' => 100,
                'summary' => 'Hide fields',
                'singular' => true,
                'autoload' => "template=admin"
                );
	}
		
	public function init() {   				
		$this->addHookBefore('Inputfield::render', $this, 'setHide');
	}
	
	public function setHide($event) {
		$inputfield = $event->object;
		if($inputfield->name != "Foo") return;
		$inputfield->label = "Bar";				// This works
		$inputfield->collapsed = Inputfield::collapsedHidden;	// This doesn't work	
	}
}

 

Link to comment
Share on other sites

Thanks fbg13 and szabesz but my purpose is to be able to control the visibility at page-level and not only at template-level.
I know there is also the possibility of inputfield dependencies but in my case this does not suit my needs.

The code above is a simplified version of my original code but shows exactly the problem. 

Link to comment
Share on other sites

3 hours ago, Alfred said:

my purpose is to be able to control the visibility at page-level

 

This works for me:

$pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) {
    if($event->object->getPage()->id !== 1016) return;
    $wrapper = $event->return;
    if(!$wrapper->has('body')) return;
    $wrapper->body->collapsed = Inputfield::collapsedHidden;
});

I have included a check for the ID of the page being edited. So in this case it will only hide the "body" field on a page with ID 1016. Hopefully you can modify that for your needs.

BTW, I just put that code in my site/init.php file - no need for a module for something so simple.

  • Like 4
Link to comment
Share on other sites

Thanks Adrian,

I was just writing the solution to my own question. In the  thread I mentioned I found out that there was a little typo that made it not work.
Your suggested code works, thank you.
Could it be though that it is not working for the title field. It is gives an error:

Notice: Indirect modification of overloaded property ProcessWire\InputfieldWrapper::$title has no effect in /…/…/…/htdocs/…/…/processwire/site/modules/HideFields.module on line 27
Warning: Attempt to assign property of non-object in /…/…/…/htdocs/…/…/processwire/site/modules/HideFields.module on line 27

For completeness this is the code that worked (except for the title field):

class HideFields extends WireData implements Module {
	public static function getModuleInfo() {
        return array(
                'title' => 'Hide Fields',
                'version' => 100,
                'summary' => 'Hide fields',
                'singular' => true,
                'autoload' => "template=admin"
                );
	}

	public function init() {   				
		$this->addHookAfter("ProcessPageEdit::buildFormContent", $this, "setHide");
	}
	
	public function setHide($event){
	 $wrapper = $event->return;	 
	 if(!$wrapper->has('Foo')) return;
	   $wrapper->Foo->collapsed = Inputfield::collapsedHidden;
	}
}

 

 

Link to comment
Share on other sites

Making small steps with programming in ProcessWire.
But thanks to guys like you (and Processwire itself of course) all the small steps take me further than I ever thought possible.

Thanks again and have a nice weekend.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...