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

18 hours ago, Flashmaster82 said:

I tried to specify the parent but i didnt worked?

It doesn't make sense to put the parent path in a check against the page template, but you can check the parent path separately:

if($page->template != 'booking') return;
if($page->parent->path !== '/nya-bokningar/') return;
// The rest of your hook code...

 

  • Thanks 1
Link to comment
Share on other sites

I have another page that im using a page reference on (booking_status), i tried with the same logic "eq(1)" but it didnt worked? Is eq only for hooks?

if ($bookingstatus->booking_status->eq(1)) {
   require ('assets/bookingstatus/bookingstatus_step2.inc.php');
   } 

I get "Exception: Method Page::eq does not exist or is not callable in this context"

Link to comment
Share on other sites

8 hours ago, Flashmaster82 said:

i tried with the same logic "eq(1)" but it didnt worked

First have a look at the docs for eq(): https://processwire.com/api/ref/wire-array/eq/

This method works on a WireArray/PageArray and returns the item at the given position. So the object you call the method on needs to be a WireArray/PageArray but you are calling it on a Page object (probably your booking_status field is a "single" Page Reference field so its value when populated will be a Page).

It's not clear to me what you're trying to do in that part of your code, but if you want your if() test to mean "if the booking_status field is not empty" then you would do something like this:

if($bookingstatus->booking_status->id) {
	//...
}

Or if you want to check if the value of the booking_status field value equals some page then you would do something like this:

// Get $some_page however you like
$some_page = $pages->get('/path/to/page/');
// Check if the booking_status field value equals $some_page
if($bookingstatus->booking_status === $some_page) {
	//...
}

 

  • Thanks 1
Link to comment
Share on other sites

Thanks for the explanation. I wanted to use the eq thing but i don´t know how? Im not that good at PHP but im learning.

Did 2 different approaches.

$status3 = $pages->get('/bokningsstatus/steg3/');
   if ($bookingstatus->booking_status === $status3) {
   require ('assets/bookingstatus/bookingstatus_step3.inc.php');
   } 

if ($bookingstatus->booking_status->id == 1738) {
   require ('assets/bookingstatus/bookingstatus_step1.inc.php');
   }

bild.png.0f1a9434472267a088dc53a93caea813.png

But i wanted to do an if statement if the status in the dropdown is at the first, second, third etc.. with the eq. Then include the inc file. Because if you change the references i wont work.

Link to comment
Share on other sites

19 hours ago, Flashmaster82 said:

But i wanted to do an if statement if the status in the dropdown is at the first, second, third etc.. with the eq.

Then you'll want to get the PageArray of selectable pages as shown in the earlier hook:

$selectable_pages = $page->getInputfield('your_page_reference_field')->getSelectablePages($page);

Then you can use ->eq() to get any item in the PageArray by its index/position, and use a switch statement to do different things accordingly.

// Get all the selectable pages that apply when "your_page_reference_field" is used on $page
$selectable_pages = $page->getInputfield('your_page_reference_field')->getSelectablePages($page);
// Compare the value of $page->your_page_reference_field against the pages in various positions of $selectable_pages
switch($page->your_page_reference_field) {
	case $selectable_pages->eq(0):
		// The first selectable page is selected
		require ('assets/bookingstatus/bookingstatus_step1.inc.php');
		break;
	case $selectable_pages->eq(1):
		// The second selectable page is selected
		require ('assets/bookingstatus/bookingstatus_step2.inc.php');
		break;
	case $selectable_pages->eq(2):
		// The third selectable page is selected
		require ('assets/bookingstatus/bookingstatus_step3.inc.php');
		break;
	default:
		// Some other page is selected so handle that scenario...
}

 

  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

Hey thanks for the special and helpful reply. I can solve my problem with the help of this thread https://processwire.com/talk/topic/27126-hook-to-select-gosloto-first-option-in-page-reference-fieldtype/. I am going to share this thread https://processwire.com/talk/topic/27126-hook-russia gosloto-to-select-first-option-in-page-reference-fieldtype/ to my friends and relatives who faced the same problem. If I want any more guideline I will contact you here https://processwire.com/talk/topic/27126-hook-gosloto results-to-select-first-option-in-page-reference-fieldtype/.

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