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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By snck
      Hello,
      in a project I use the multi-instance feature of ProcessWire to get content from another ProcessWire instance which is kind of a "database". I do not modify the external content. For a slider I want to store references to the "database" in an InputfieldPage. To get the available pages I use the following code in my ready.php:
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) { if($event->object->hasField == 'kub_highlight_artworks') { // Server path to the PW installation $path = '/var/www/vhosts/.../'; // The root URL for the PW installation $url = 'https://.../'; // Create a new ProcessWire instance $db = new ProcessWire($path, $url); $event->return = $db->pages->find("template=artwork, sort=title"); } }); Outputting and selecting the external pages works perfectly fine, but unfortunately the selected pages do not get saved when I save the page.
      Can someone point me into the right direction how to make this work? Are there any internal checks whether the page exists in the current instance that prevent saving the values?
      Best,
      Flo
    • By Robin S
      Page Reference Default Value
      Most ProcessWire core inputfield types that can be used with a Page Reference field support a "Default value" setting. This module extends support for default values to the following core inputfield types:
      Page List Select Page List Select Multiple Page Autocomplete (single and multiple) Seeing as these inputfield types only support the selection of pages a Page List Select / Page List Select Multiple is used for defining the default value instead of the Text / Textarea field used by the core for other inputfield types. This makes defining a default value a bit more user-friendly.
      Note that as per the core "Default value" setting, the Page Reference field must be set to "required" in order for the default value to be used.
      Screenshot

       
      https://github.com/Toutouwai/PageReferenceDefaultValue
      https://modules.processwire.com/modules/page-reference-default-value/
    • By MilenKo
      Hello all.
      A few days ago I decided to redo my first PW theme (knowledge sharing one) trying to apply some more elegant logic and most important - to learn to filter page results by a Page reference field with some values. So far it is all going well except the fact that I am now at a stage to add some page parameters and am a bit stuck as to how to filter them.
      Here is an example of the structure planned to use:
      !-- Articles (template = articles.php)
      !---- Article 1 (template = articles-inner.php)
      !---- Article 2 ..
      !---- Article 3 ..
      !-- Categories (no template file)
      !---- Category1 (same template as  parent)
      !---- Category2 ..
      !---- Category3 ..
      !-- OS (no template file)
      !---- Windows (same template as parent)
      !---- Linux
      !---- MacOS
      So far the home page and articles-inner pages are fully working. I need to show in the sidebar two blocks; Categories, OS where clicking on one would show only the posts containing the PageReference field value. To list the categories and OS in the block won't be a problem as I would just need to query the parent and list all the children (if any) with the proper markup. However, how to have the Categories and OS blocks link only to the specific posts that is the issue. In my previous version of the knowledge profile, I had the "categories" listed as a parent and the posts matching the category were just children of it. But now my goal is to learn how to add different parameters using Page Reference and have a way to show the results per the specific value.
      How would you organize a similar structure and what approach to take to have a simple and elegant solution?
      Or, if you know of a PW profile that I could dissect and learn the filtering mechanism of Page Reference etc. it would be even better. I am presently looking at the Ghost profile however Wire-approach is still not very much clear to me, unfortunately.
       
    • By dragan
      What could be the reasons why not all pages appear in my page referefence field with page autocomplete?
      I have a dozen pages that should appear, with a 4-letter string that never show up.
       
      My selector should be fine:
      parent=1041, template=project, id!=id, sort=vertec, include=all Where does it actually look for the suggestions? only default page-title? I tried with title and several other fields, also custom variations for the label, e.g.
      {vertec}, {title}, {client_name} {year} Q1: In which fields does PW look for autosuggestions? only title? Can it be configurable to look also elsewhere (let's say 3 text fields)
      Q2: Does my custom labeling actually change anything in the behavior of autosuggest? (I guess not)
       
      In my case, I have over 1000 pages under one parent. Using the other option "page-selector" is not a real option (paginate several times...)
    • By antoiba86
      Well, I am trying to put the blog template like the home template but when I change the home template to the blog template the page explote. The version is processwire 3.055 and this is the website.
      http://www.antoibaprogramming.com/
      I think that the error is because that when is the home, the blog template try to look for the blog post but it didn't find anything so it crashes.
      Here is the error that it gave me.
      And here is the code. The line 660 is the "$n = $page->comments->count();"
      function ukBlogPost(Page $page, $options = array()) { $defaults = array( 'summarize' => null, // Display blog post summary rather than full post? (null=auto-detect) 'metaIcon' => 'info', 'moreIcon' => 'arrow-right', 'moreText' => __('Read more'), 'categoryIcon' => 'hashtag', 'bylineText' => __('Posted by %1$s on %2$s'), ); $options = _ukMergeOptions($defaults, $options); $title = $page->title; $date = $page->date ? $page->date : $page->createdStr; $name = $page->createdUser->name; $body = $page->body; $metaIcon = ukIcon($options['metaIcon']); $moreIcon = ukIcon($options['moreIcon']); $categoryIcon = ukIcon($options['categoryIcon']); $n = $page->comments->count(); $numComments = $n ? "<a href='$page->url#comments'>" . ukIcon('comments') . " $n</a>" : ""; if($options['summarize'] === null) { // auto-detect: summarize if current page is not the same as the blog post $options['summarize'] = page()->id != $page->id; } $categories = $page->categories->each($categoryIcon . "<a class='uk-button uk-button-text' href='{url}'>{title}</a> " ); if($options['summarize']) { // link to post in title, and use just the first paragraph in teaser mode $title = "<a href='$page->url'>$title</a>"; $body = explode('</p>', $body); $body = reset($body) . ' '; $body .= "<a href='$page->url'>$options[moreText] $moreIcon</a></p>"; $class = 'blog-post-summary'; } else { $class = 'blog-post-full'; } if($options['summarize']) { $heading = "<h2 class='uk-margin-remove'>$title</h2>"; } else { $heading = "<h1 class='uk-article-title uk-margin-remove'>$title</h1>"; } $byline = sprintf($options['bylineText'], $name, $date); // return the blog post article markup return " <article class='uk-article blog-post $class'> $heading <p class='uk-margin-small'> <span class='uk-article-meta'> $metaIcon $byline </span> <span class='categories'> $categories </span> <span class='num-comments uk-margin-small-left uk-text-muted'> $numComments </span> </p> $body </article> <hr> "; }  
      The question is how can I make to put the blog like the principal page. I don't have any idea how to do it.
×
×
  • Create New...