Jump to content

Selecting the Home Page with Page Autocomplete


Jason Huck
 Share

Recommended Posts

I have a Page field with no restrictions on selection, set to use Page Autocomplete. Administrators need to be able to select the home page just like any other. When they type "Home" into the field, the home page appears as an option, but when it is selected, nothing happens.

There aren't any javascript errors, and other pages can be added as expected. I do notice in the AJAX call to get the menu results there is a query param "id>0" but "Home" does appear in the list, it just can't be selected.

Page Autocomplete is the only practical UI option due to the large (over 6k and growing) number of pages in the site.

Has anyone else seen this behavior? Is this a bug or am I missing something in the configuration? This is on PW 2.7.3.

  • Like 1
Link to comment
Share on other sites

I took a really quick peek inside /wire/modules/Inputfield/InputfieldPage/InputfieldPage.module, and am wondering if this block, starting at line 175 within the isValidPage method, is where the problem lies?

		if($field->parent_id && $field->parent_id != $page->parent_id) {
			if(version_compare(PHP_VERSION, '5.3.8') >= 0) {
				if(is_subclass_of($field->inputfield, 'InputfieldPageListSelection')) {
					// parent_id represents a root parent
					$rootParent = wire('pages')->get($field->parent_id); 
					if(!$page->parents()->has($rootParent)) $valid = false;
				} else {
					// parent_id represents a direct parent
					$valid = false;
				}
				if(!$valid && $editPage) $editPage->set('_isValidPage', "Page $page does not have required parent $field->parent_id"); 
			} else {
				// PHP version prior to 5.3.8
				$reflector = new ReflectionClass($field->inputfield); 
				$valid = $reflector->implementsInterface('InputfieldPageListSelection'); 
			}
		}

Specifically, this line (180):

					if(!$page->parents()->has($rootParent)) $valid = false;

Would that prevent the home page from validating, since it doesn't have a root parent (parent_id is 0)? Seems like we need an exception for the home page here, yes?

  • Like 1
Link to comment
Share on other sites

No, the true culprit is here:

	protected function ___renderList() { 

		$out = "\n<ol id='{$this->id}_items' data-id='{$this->id}' data-name='{$this->name}'>" . 
			$this->renderListItem("Label", "1", "itemTemplate"); 

The pageSelected function in InputfieldPageAutocomplete.js then checks if the select page's id equals the value in the item template, and with "1" matching the id of "home", it returns without appending the page. I can't definitely say if this is intentionally excluding home, but my guess is that it was only meant to exclude items already present in the list in whatever scenario those might occur. In that case, excluding the template item in the selector for the duplicate checks would be sufficient:

	pageSelected: function($ol, page) {

		var dup = false;
		
		$ol.children('li:not(.itemTemplate)').each(function() {
			var v = parseInt($(this).children('.itemValue').text());	
			if(v == page.page_id) dup = $(this);
		});

 

  • Like 5
Link to comment
Share on other sites

22 hours ago, BitPoet said:

No, the true culprit is here:


	protected function ___renderList() { 

		$out = "\n<ol id='{$this->id}_items' data-id='{$this->id}' data-name='{$this->name}'>" . 
			$this->renderListItem("Label", "1", "itemTemplate"); 

The pageSelected function in InputfieldPageAutocomplete.js then checks if the select page's id equals the value in the item template, and with "1" matching the id of "home", it returns without appending the page. I can't definitely say if this is intentionally excluding home, but my guess is that it was only meant to exclude items already present in the list in whatever scenario those might occur. In that case, excluding the template item in the selector for the duplicate checks would be sufficient:


	pageSelected: function($ol, page) {

		var dup = false;
		
		$ol.children('li:not(.itemTemplate)').each(function() {
			var v = parseInt($(this).children('.itemValue').text());	
			if(v == page.page_id) dup = $(this);
		});

 

I can confirm that this solves the issue for me in PW 2.7 with no apparent side effects thus far. Will report back if I discover otherwise. Thanks!

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