Jump to content

Change Fieldtype collapsed/locked from API


Federico
 Share

Recommended Posts

Hi all,

I am having trouble on setting a fieldtype visibility from API, I need to set it as locked on a page save. here is the code from the module, which is called on init() 

$this->addHookBefore("Inputfield::render", $this, "renderFieldFolder");

Now, everything's ok except the locked feature, I can collapse or open the field from API but no way to lock it

public function renderFieldFolder(HookEvent $event) {
  $field = $event->object;

  if($field->name == 'proj_code_folder') {
  $field->collapsed = Inputfield::collapsedNoLocked;
  } 
}

Any clue? 

  • Like 1
Link to comment
Share on other sites

For some inputfield visibility constants it is too late to set them at inputfield render. For Inputfield::collapsedHidden, Inputfield::collapsedNoLocked and Inputfield::collapsedYesLocked these are handled by the InputfieldWrapper class.

I've found that a good method to hook for setting the visibility of fields in Page Edit is Field::getInputfield. This example is for /site/ready.php but easy to adapt to a module context:

$wire->addHookAfter('Field::getInputfield', function(HookEvent $event) {
    // Only for ProcessPageEdit
    if($this->page->process !== 'ProcessPageEdit') return;
    $field = $event->object;
    $inputfield = $event->return;
    if($field->name == 'proj_code_folder') {
        $inputfield->collapsed = Inputfield::collapsedNoLocked;
    }
});

 

  • Like 6
  • Thanks 1
Link to comment
Share on other sites

  • 7 months later...
  • 9 months later...
On 11/29/2017 at 9:36 PM, Robin S said:

I've found that a good method to hook for setting the visibility of fields in Page Edit is Field::getInputfield. This example is for /site/ready.php but easy to adapt to a module context:

Hi @Robin S

I've just tried your example, because I'm always using ProcessPageEdit::buildForm for such things. In my case your hook fires twice on a regular page edit and three times on an multilang field with two languages... Any reason why you are using Field::getInputfield and not ProcessPageEdit::buildForm like in this example that I've just posted:

 

 

  • Like 1
Link to comment
Share on other sites

6 hours ago, bernhard said:

Any reason why you are using Field::getInputfield and not ProcessPageEdit::buildForm like in this example that I've just posted

No particular reason as far as I remember - probably I was just trying different things and stopped as soon as I found a way to get the job done. Usually in PW there are several different way to achieve any goal.

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...