Jump to content

Hook to ProcessPageEdit::buildForm and Repater field


Recommended Posts

Posted

Hi.

I have a hook to ProcessPageEdit::buildForm where I add a custom tab. 

wire()->addHookAfter('ProcessPageEdit::buildForm', function ($event) {
	$page = $event->object->getPage();

	if ($page->template == "news-item" || $page->template == 'organization-news-item') {
		
		//....

		$pageEditTab = $form->find('id=ProcessPageEditContent')->first();
		$form->insertAfter($inputfields, $pageEditTab);

		wire()->addHookAfter('ProcessPageEdit::getTabs', function ($event) {
			$event->return = array_merge(
				array_slice($event->return, 0, 1, true),
				array('change_news_parent' => __('Изменить категорию новости')), // should be identical to the weird name/title above
				array_slice($event->return, 1, null, true)
			);
		});

		$event->return = $form;
	}
});

I have repeater field in the template for these pages.

The issue is that this hook is also triggered when I add a new item in the repeater field. So the $form becomes the form of repeater page and this part of the code return null

$form->find('id=ProcessPageEditContent')->first()

and I get the error about the second argument inside insertAfter method.

I can add if (!$pageEditTab) return;, but it looks unclear for me how I can limit my code only for 'master' page and not items of repeater field.

Thank in advance.

Posted
10 hours ago, Zeka said:

I can add if (!$pageEditTab) return;

That sounds like a reasonable way to return early if the form is not the one you are wanting to modify. Any problems if you do that?

  • Like 1
  • Thanks 1
Posted

@Robin S No, there are no problems and actually, I use this code, but I would like to have a more obvious way to detect for which page the hook was triggered.  

$page instanceof RepeaterPage 
or
$page->template != "repeater_template"

 

  • Thanks 1
  • 2 years later...
Posted

Thank you to both @Zeka and @Robin S.

I finally troubleshot my own head-scratcher to the point I was able to search for the right thing and, of course, was not surprised to find Robin involved in the exact answer I was looking for.

You are both legends.

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