Jump to content

[SOLVED] Hook runs twice


Nicolas
 Share

Recommended Posts

Hi,

For a project I need to add a custom admin action button. Tanks to @bernhard i was able to add the action in the submit button dropdown.

To peform the action i'm adding a hook to the InputfieldSubmit::processInput method, however the function is executed twice for some reasons i can't figure out.

What am i doing wrong  ?

 

<?php
// Add a save action button to resend the registration email
$wire->addHookAfter('ProcessPageEdit::getSubmitActions', function(HookEvent $event) {
    $actions = $event->return;
    $actions['send_registration'] = [
        'value' => 'send_registration',
        'icon' => 'check',
        'label' => '%s + Resend registration email',
    ];
    $event->return = $actions;
});

// Process custom save button action
wire()->addHookAfter('InputfieldSubmit::processInput', null, 'send_registration_email');
function send_registration_email($event) {
  $input = $event->arguments(0);

  if('send_registration' === $input->_after_submit_action) {
      // populate  email vars here
    send_email($input->email, $mail_subject, $mail_body, wireMail());

  }
  // Populate back return value, if you have modified it
  // $event->return = $attendee;
  $event->cancelHooks = true;
};

function send_email($email, $mail_subject, $mail_body, $mailer) {
    // Sends the email
}


 

Edited by kongondo
Wrapped code in code blocks <>
Link to comment
Share on other sites

On 11/19/2020 at 11:30 PM, Nicolas said:

however the function is executed twice for some reasons i can't figure out

Using Tracy Debugger to dump the name of the submit button can give you a clue...

$wire->addHookAfter('InputfieldSubmit::processInput', function(HookEvent $event) {
	$inputfield = $event->object;
	bd($inputfield->name, "inputfield->name");
});

2020-11-20_235413.png.5319b576f22f85655bdb9c7522c536aa.png

So there are two submit buttons in Page Edit that get processed: the Save button and the Trash button on the Delete tab.

Rather than hooking InputfieldSubmit::processInput you can use the dedicated hookable method that performs the save actions:

$wire->addHookAfter('ProcessPageEdit::processSubmitAction', function(HookEvent $event) {
	$value = $event->arguments(0);
	if($value === 'send_registration') {
		// Do your action here...
	}
});

 

  • Like 2
Link to comment
Share on other sites

  • Nicolas changed the title to [SOLVED] Hook runs twice

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