Jump to content

Recommended Posts

Posted

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

Posted
// 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
Posted

Hi @Juergen

I'm not sure about 

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

But you can define function separately

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

 

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

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

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
Posted

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
Posted

In my case I want to count the children which are under the current page beeing edited. Each child page is a date, so I count of how many dates this page has.

But counting the children via siblings could also be an opportunity.

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
  • Recently Browsing   0 members

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