Jump to content

Is there a way to combine hooks to reduce the code


Juergen
 Share

Recommended Posts

Hello @ all,

In my case I have some hooks with the same code which run on page "Pages::trash" and "Pages::delete" inside may ready.php. For the moment I have used separate hooks for each of them.

Is there a possibility to combine them so the same code runs only once and includes "trash" and "delete"?

Explanation:

Instead of

$wire->addHookBefore("Pages::trash", function($event) {
.... run code
});

and

$wire->addHookBefore("Pages::delete", function($event) {
.... run code
});

combine them

$wire->addHookBefore("Pages::trash","Pages::delete", function($event) {
.... run code
});

This would be more efficiently for further changes and reduces the code.

Best regards

Link to comment
Share on other sites

// inside a class e.g. modules
$this->addHookAfter('Pages::trash', $this, 'myHookMethodName');
$this->addHookAfter('Pages::delete', $this, 'myHookMethodName');

public function myHookFunction($event) {
  // ...
}

// outside a class e.g. inside ready.php or templates
wire()->addHookAfter('Pages::trash', null, 'myHookFunctionName');
wire()->addHookAfter('Pages::delete', null, 'myHookFunctionName');

function myHookFunction($event) {
  // ...
}

 

  • Like 5
Link to comment
Share on other sites

11 minutes ago, Zeka said:

wire()->addHookAfter('Class::method', null, 'myHookFunctionName');

Thanks @Zeka,

but I guess in this case I have to define the functions for each "Class::method" - so I have also 2 functions ("Pages::trash", "Pages::delete") - so I can leave the 2 hooks as they are.

Link to comment
Share on other sites

27 minutes ago, kixe said:

// outside a class e.g. inside ready.php or templates wire()->addHookAfter('Pages::trash', null, 'myHookFunctionName'); wire()->addHookAfter('Pages::delete', null, 'myHookFunctionName'); function myHookFunction($event) { // ... }

Aaahh, I see!! This would be a solution :) Thanks!!!

  • Like 1
Link to comment
Share on other sites

Here is my code for others who also deals with the same issue.

This little function prevents the deletion of the last child page if page has a certain template. Code runs inside ready.php.

//prevent deletion of last child function
wire()->addHookBefore('Pages::trash', null, 'preventdeletionlastevent');
wire()->addHookBefore('Pages::delete', null, 'preventdeletionlastevent');

function preventdeletionlastevent($event) {
$page = $event->arguments(0);
if(!in_array($page->template->name, ['single-date', 'single-event', 'single-business-vacation', 'single-special-business-hours'])) return;
$parent = $page->parent;
$childrennumber = count($page->parent->children);
if($childrennumber === 1) {
 $event->replace = true; // now original function won't be called
 $event->return = wire()->warning(__("Deleting of the last date is not allowed. There must be at least 1 date."));
} else {
 wire()->message(__("1 date was deleted."));
}
}

 

  • Like 2
Link to comment
Share on other sites

I wonder why you need something like this in the first place "to have at least one date". 

wire()->addHookBefore('Pages::trash', null, 'hookTrashDelete');
wire()->addHookBefore('Pages::delete', null, 'hookTrashDelete');

function hookTrashDelete($event) {
	$page = $event->arguments("page");

	//prevent deletion of last child
	$preventTpls = [
		'single-date',
		'single-event',
		'single-business-vacation',
		'single-special-business-hours'
	];
	if(in_array($page->template, $preventTpls)) {
		preventDeleteDateEntry($event, $page);
	}
}

function preventDeleteDateEntry($event, $page){
	if($page->parent->numChildren() === 1) {
		$event->replace = true; // now original function won't be called
		wire()->warning(__("Deleting of the last date is not allowed. There must be at least 1 date."));
	} else {
 		wire()->message(__("1 date was deleted."));
	}
}

I would code it like this. So the hook could be used for different things.

Also some code formatting for better reading.

Use $page->numChildren(), in case you have thousands/millions of children you'd get a server out of memory 

Don't use $event->return = error.

  • Like 3
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...