Jump to content

How to hook after field configuration is saved [SOLVED]


Juergen
 Share

Recommended Posts

Hello @all

I have searched the forum and studied the PHP class files, but I cannot find a way to achieve the following one:

I want to run a Hook after the pass field (a system field) is saved. It should also work with other inputfields, but in my case I need to run it especially on the pass field.

To clearify: I want to run the Hook if the "Edit field: pass" page is saved. I have tried the following inside the init() function of my module:

2048496580_Screenshot2022-12-10at19-55-05EditFieldpasswebseite1_at.thumb.png.5a19a8ebd78bf2b5a8cf9386f76c89bb.png

$this->addHookAfter('Fields::save', $this, 'updateBlacklist');

The appropriate method:

protected function updateBlacklist(HookEvent $event): void
    {
        $field = $field->object;
        if ($field->name == 'pass') {
            $this->wire('session')->message('works');
        }
    }

But it does not work.

As mentioned by @LostKobrakai at this topic, " Field::save is called when a fieldsettings should be saved", but it seems that nothing happens.

Could anyone point me into the right direction?

 

Link to comment
Share on other sites

45 minutes ago, Juergen said:
$this->addHookAfter('Fields::save', $this, 'updateBlacklist');

Note that you’re hooking the save() method of “Fields” object.

45 minutes ago, Juergen said:
$field = $field->object;

This line makes no sense. I assume you mean “$event->object”, but because you’re hooking a method of the Fields class, I would imagine that will give you the same as wire()->fields(), not the field that was saved. The saved field is the first and only argument of the save() method, so I expect this should work:

$field = $event->arguments(0);

 

 

Apparently, towards the end, Fields::save() also calls Fieldtype::savedField() , so you may want to hook that instead:

$this->addHookAfter('FieldtypePassword::savedField', $this, 'updateBlacklist');
protected function updateBlacklist(HookEvent $event): void
{
    $field = $events->arguments(0);
    if ($field->name == 'pass') {
        $this->wire('session')->message('works');
    }
}

 

  • Like 2
Link to comment
Share on other sites

Thanks @Jan Romero

What a silly mistake!!! ??You are right:

1 hour ago, Jan Romero said:

I assume you mean “$event->object”,

Sure, that was the mistake. I know that it must be $event and not $field, but I have overlooked this silly mistake over and over and I was wondering why it did not work. I have tried it with $event->object and $event->arguments(0)....

BTW: Now both Hooks work with $event->arguments(0)!!

Thanks for your help!!!

Thanks for helping me

  • Like 1
Link to comment
Share on other sites

  • Juergen changed the title to How to hook after field configuration is saved [SOLVED]

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...