Jump to content

PageField custom PHP code returning blank


a-ok
 Share

Recommended Posts

Hi folks,

I want to return a few repeater items via a PageField and originally I would do this by writing the commented out code in the PHP area provided BUT now it states I should be doing this in ready.php via a hook.

I have written the below... following from the example given in the backend but it's not returning anything in the PageField dropdown. The message I set up to do a bit of debugging seems to be returning the correct items.

Any thoughts? And am I able to return the title of the repeater item being chosen as the getForPage()->title as shown below (how I previously did it).

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
	if ($event->object->name == 'home_whatson_article') {

		$event->return = $event->pages->find("template=repeater_events_detail_dates, events_detail_dates_start_date>today, sort=events_detail_dates_start_date");

		$this->message($event->return);

		/*
		$options = new PageArray();
		foreach ($pages->find("template=repeater_events_detail_dates, events_detail_dates_start_date>today, sort=events_detail_dates_start_date") as $e) {
			$e->title = $e->getForPage()->title;
			$options->add($e);
		}
		return $options;
		*/


	}
});

 

Link to comment
Share on other sites

InputfieldPage::getSelectablePages doesnt work with all input types

	protected static $defaultInputfieldClasses = array(
		'InputfieldSelect', // works
		'InputfieldSelectMultiple', // works
		'InputfieldCheckboxes', // works
		'InputfieldRadios',  // works
		'InputfieldAsmSelect', // works
		'InputfieldPageListSelect',  // doesnt work
		'InputfieldPageAutocomplete', // doesnt work
		);

Because it's called only if inputfield has addOption() method

// InputfieldPage.module
/**
 * Get delegate Inputfield for page selection
 * 
 * @return Inputfield|null
 * @throws WireException
 * 
 */
public function getInputfield() {
    // ...
    if(method_exists($inputfield, 'addOption')) {
        
        $children = $this->getSelectablePages($page);
        
        if($children) {
            foreach($children as $child) {
                $label = $this->getPageLabel($child);
                $inputfield->addOption($child->id, $label);
            }
        }
        
    } 
    // ...
}

 

Link to comment
Share on other sites

1 minute ago, abdus said:

InputfieldPage::getSelectablePages doesnt work with all input types


	protected static $defaultInputfieldClasses = array(
		'InputfieldSelect', // works
		'InputfieldSelectMultiple', // works
		'InputfieldCheckboxes', // works
		'InputfieldRadios',  // works
		'InputfieldAsmSelect', // works
		'InputfieldPageListSelect',  // doesnt work
		'InputfieldPageAutocomplete', // doesnt work
		);

Because it's called only if inputfield has addOption method()


// InputfieldPage.module
/**
 * Get delegate Inputfield for page selection
 * 
 * @return Inputfield|null
 * @throws WireException
 * 
 */
public function getInputfield() {
    // ...
    if(method_exists($inputfield, 'addOption')) {
        
        $children = $this->getSelectablePages($page);
        
        if($children) {
            foreach($children as $child) {
                $label = $this->getPageLabel($child);
                $inputfield->addOption($child->id, $label);
            }
        }
        
    } 
    // ...
}

 

Thanks, abdus. My PageReference field is InputfieldAsmSelect :(

Link to comment
Share on other sites

I tried using asmSelect, it works fine on my setup. Maybe you need include=all (repeater pages are hidden by default)?

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    if($event->object->hasField == 'tags') {
        $event->return = $event->pages->find('template=repeater_seo, include=all');
    }
});

59c285bf89db7_pagefield.gif.7be34fc7189326c4232ae8060325f9a9.gif

Link to comment
Share on other sites

4 minutes ago, abdus said:

I tried using asmSelect, it works fine on my setup. Maybe you need include=all (repeater pages are hidden by default)?


$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    if($event->object->hasField == 'tags') {
        $event->return = $event->pages->find('template=repeater_seo, include=all');
    }
});

59c285bf89db7_pagefield.gif.7be34fc7189326c4232ae8060325f9a9.gif

I even changed it just to find all pages with a specific template (not a repeater) AND to a simple Select field (not ASM) and I get nothing.

My PageReference field is in a repeater... could this be something that is killing it?

Link to comment
Share on other sites

2 minutes ago, abdus said:

 

Sorry it's the same field... for testing purposes I just tried to find events-detail templates. If you stick your example in a repeater it won't work.

$event->return = $event->pages->find("template=events-detail");

 

Link to comment
Share on other sites

It works fine inside repeaters too

image.png.1a316db2fe32c1b791f686f74b58634d.png

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    if($event->object->hasField == 'pageRef') {
        $event->return = $event->pages->find('template=repeater_testRepeater, include=all');
    }
});

 

  • Like 1
Link to comment
Share on other sites

2 minutes ago, abdus said:

It works fine inside repeaters too

image.png.1a316db2fe32c1b791f686f74b58634d.png


$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    if($event->object->hasField == 'pageRef') {
        $event->return = $event->pages->find('template=repeater_testRepeater, include=all');
    }
});

 

Thanks for testing, abdus.

It turns out my line if ($event->object->name == 'home_whatson_article') { needed to be your line if ($event->object->hasField == 'home_whatson_article') { :(

  • Like 1
Link to comment
Share on other sites

3 hours ago, oma said:

It turns out my line if ($event->object->name == 'home_whatson_article') { needed to be your line if ($event->object->hasField == 'home_whatson_article') { :(

Ryan committed a change back in May in response to this issue but it hasn't been merged to the master branch yet.

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