Jump to content

Hook to select first option in page reference fieldtype


Flashmaster82
 Share

Recommended Posts

I want to select the first option in a page reference fieldtype when adding a new page (admin). Dont want to target the option id because the value may change.

Field = booking_searchactivator_option

Template = booking

Page reference template = searchactivator

My current code is only targeting the page id and not the value. So now it becomes id=1. Also i want to be able to change the value to the second value, third value / position in the dropdown.

  $wire->addHookAfter('Pages::added', function(HookEvent $event) {
   $page = $event->arguments(0);
    
   if (!$page->matches('template=booking')) return; 
     
     $aktivator = 1;
   
     $page->setAndSave('booking_searchactivator_option', $aktivator);  
  
});

 

i also have another hook that removes already selected page references.

 $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
        $page = $event->arguments(0);
            if($event->object->hasField == 'booking_searchactivator_option') {
                $pages = $event->wire()->pages;
                $already_selected_on_another_page = $pages->find("template=searchactivator, booking_searchactivator_option.owner.template=booking, booking_searchactivator_option.owner.id!=$page->id");
    $event->return = $event->pages->find("template=searchactivator, sort=sort, id!=$already_selected_on_another_page");
	}
    });

 

Please help!!

Link to comment
Share on other sites

  • Flashmaster82 changed the title to Hook to select first option in page reference fieldtype
On 5/15/2022 at 4:54 AM, Flashmaster82 said:

Also i want to be able to change the value to the second value, third value / position in the dropdown.

If you want to pre-set the third option within the selectable pages:

$wire->addHookAfter('Pages::added', function(HookEvent $event) {
	$page = $event->arguments(0);

	// Only for the booking template
	if($page->template != 'booking') return;
	// Get all the selectable pages for the field
	$selectable_pages = $page->getInputfield('booking_searchactivator_option')->getSelectablePages($page);
	// Get the third selectable page (the numbering is zero-based)
	$third_selectable_page = $selectable_pages->eq(2);
	// Set the third selectable page as the field value
	$page->setAndSave('booking_searchactivator_option', $third_selectable_page);
});

 

Edited by Robin S
corrected field name
  • Thanks 1
Link to comment
Share on other sites

It worked like a charm. Thank you so much!!!

I tried to specify the parent but i didnt worked?

$wire->addHookAfter('Pages::added', function(HookEvent $event) {
	$page = $event->arguments(0);
	if($page->template != 'booking, parent=/nya-bokningar/') return;
	$selectable_pages = $page->getInputfield('booking_searchactivator_option')->getSelectablePages($page);
	$first_selectable_page = $selectable_pages->eq(0);
	$page->setAndSave('booking_searchactivator_option', $first_selectable_page);
});

 

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

×
×
  • Create New...