Jump to content

custom php code to find selectable pages + autocomplete


bernhard
 Share

Recommended Posts

hi,

i have a page field with the following setup:

post-2137-0-51190500-1431683988_thumb.pn

the description says it is not compatible with autocomplete. i tried it anyway and it did almost work. with the $page->rootParent selector it let me select all pages under my rootparent (/section1) with template basic-page. i added one testpage with template "sitemap" and this one does not show up in the autocomplete list.

with pagelistselectmultiple it works as expected! can anybody elaborate why this strange behaviour occurs or if there is a chance to get this work in any future releases?

Link to comment
Share on other sites

If it explicitly states that it is incompatible with certain Inputfield types the behavior can hardly be described as strange. Maybe in your case it does work somewhat because of the type of custom PHP code, but don't expect it to work in every situation. Although it would be nice if it could, i agree.

(If we are talking about a default install here i seem to remember that the sitemap page has the 'hidden' setting turned on. Maybe this leads to PageAutocomplete to not show it.)

Link to comment
Share on other sites

The incompatibilities come from different issues. The PageList ones just cannot show anything besides a pagelist. With Autocomplete I think it's because of the ajax functionality. Both could work with the addition of the "custom php code" returning a single Page, which could then work like the simple parent selection above, but PageArrays bring their difficulties.

As for your try: Did you select a template/parent above the "custom php code" section?

Link to comment
Share on other sites

If it explicitly states that it is incompatible with certain Inputfield types the behavior can hardly be described as strange. Maybe in your case it does work somewhat because of the type of custom PHP code, but don't expect it to work in every situation. Although it would be nice if it could, i agree.

(If we are talking about a default install here i seem to remember that the sitemap page has the 'hidden' setting turned on. Maybe this leads to PageAutocomplete to not show it.)

of course it states it explicitly and i don't expect it to work in every situation, but when i know why it does not work in which situation it could fit for my needs even if it states id does not work. that was the reason for my question. you are right about the sitemap - its a custom sitemap page, not hidden. thanks for that input though.

The incompatibilities come from different issues. The PageList ones just cannot show anything besides a pagelist. With Autocomplete I think it's because of the ajax functionality. Both could work with the addition of the "custom php code" returning a single Page, which could then work like the simple parent selection above, but PageArrays bring their difficulties.

As for your try: Did you select a template/parent above the "custom php code" section?

would be great for me if they worked with single pages! i did try it with and without selected template above

Link to comment
Share on other sites

It says custom PHP code to find pages to select from and not a parent. So $page->rootParent doesn't work with custom php code to begin with. It has to return a PageArray.

Link to comment
Share on other sites

soma, that was stated in older versions but not in the new ones...

PW 2.5.21

If you want to find selectable pages using a PHP code snippet rather than selecting a parent page or template (above) then enter the code to find the selectable pages. This statement has access to the $page and $pages API variables, where $page refers to the page being edited. The snippet should return either a PageArray or NULL. Using this is optional, and if used, it overrides the parent/template/selector fields above. NOTE: Not compatible with PageListSelect or Autocomplete input field types.

PW 2.6.0

If you want to find selectable pages using a PHP code snippet rather than selecting a parent page or template (above) then enter the code to find the selectable pages. This statement has access to the $page and $pages API variables, where $page refers to the page being edited. The snippet should return either a PageArray, Page or NULL. If it returns a Page, children of that Page are used as selectable pages. Using this is optional, and if used, it overrides the parent/template/selector fields above. While this overrides parent and template selections above, those selections are still used for validation (de-select them if you do not want that behavior). NOTE: Not compatible with PageListSelect or Autocomplete input field types.
Link to comment
Share on other sites

Ah didn't knew this new description (but there were some changes regarding this fieldtype), but it remains that it doesn't work with AutoComplete or PageListSelect input.

Link to comment
Share on other sites

What functionality? :D There's been so many changes during dev I can't keep track of them.

Well it seems then if you specify only a Page (parent) and not a PageArray via custom PHP code, it works as it takes that as a parent.

It's the same as if you use the custom selector: "parent=page".

Edit: Just tested and it works with "parent=page.id" selector but only with Select input and not PageListSelect (as stated) but also not for AutoComplete . Still way to go..

Edit: amazed that such an important and most often used fieldtype is still so quirky.

Link to comment
Share on other sites

it seems to work differently because the result is different:

has_parent=1015

-> result for "site" = "site1 content" and "sitemap"

(that's a correct result)

return $page->rootParent;

-> result for "site" = "site1 content"

(missing sitemap from site1 branch)

so for me it would be perfect to be able to use the "has_parent" selector with a dynamic ID set to the rootparent of the current page.

i tried using this selector: has_parent=rootparentfield

where rootparentfield is a simple textfield storing the id of its rootparent. i thought i could maybe populate this field via hook (or lostkobrakais new module) but the selector doesn't work (result shows pages from other branches as well)

Link to comment
Share on other sites

so for me it would be perfect to be able to use the "has_parent" selector with a dynamic ID set to the rootparent of the current page.

Seems like you already had a look at my module. It does exactly what you need, but I had an error in the README file. You'd need to use this selector:

has_parent=page.rootparentfield
Link to comment
Share on other sites

thanks soma for that advise.

@lostkobrakai

hm... can't get it to work. i'm using this selector in my pagefield setup:

has_parent=page.rootparent

rootparent is a reference field (we are talking about this module: https://github.com/LostKobrakai/FieldtypeReference) with php code

return $page->rootParent;

and template field pairing

basic-page=rootparentid

i created field "rootparentid" because i was not able to retrieve basic-page=id

result:

the field shows up in the page and shows "1015" - that's the value i put in the rootparentid field on the wanted page. of course this should be dynamically set in the end, but for testing it even does not work with hard values. it also does not work with pagelistselect, so it's not related to autocomplete pagefield.

can you please explain how to set up your module for the second use case (https://github.com/LostKobrakai/FieldtypeReference#page-fields-using-the-selector-value-option). thanks!

Link to comment
Share on other sites

You just listed all the step I took and it's working for me. Just to make it clear: The rootparent field is on the same page as the page field you're setting up. If the field in the backend shows the right value, then it should work. To debug this you could try replacing the reference field with a simple integer field, while I suspect that writing out the pages id in the selector does work as expected.

Link to comment
Share on other sites

hm. i have a fresh 2.6.0 installation.

  • created pagefield with autocomplete and added it to basic-page template
  • created pages /test, /test/child-1, /test/child-2
  • set selector to parent=1001
    works! "child" finds "child page example 1+2" from the intermediate profile
  • created integer field "rootparent_int" and added it to basic-page template
  • put 1001 into that field and saved the page
  • set selector to parent=page.rootparent_int
  • tried "child" as search for autocomplete and got no results
  • also tried parent=rootparent_int, also no results

reference module is installed, though i don't know if that is required for the steps above? what am i doing wrong? :(

sorry for that many questions for such a small issue and thank you for your patience!

Link to comment
Share on other sites

InputfieldAutocomplete won't work, because it's using the ajax api of the page search in the admin. There's certainly no page context there you could build on. Now I got it to work with some small changes:

protected function getAjaxUrl() {

  $page = $this->page; 
  $process = $this->wire('process'); 
  if($process && $process->className() == 'ProcessPageEdit') $page = $process->getPage();

  $selector = InputfieldPage::getFindPagesSelector($page, $this->findPagesSelector); 

  // … rest of the function like it was
}

And additionally making InputfieldPage::getFindPagesSelector a public function instead of protected. This way the selctors "page.something" is evaluated and changed before the search url is rendered to the field.

Link to comment
Share on other sites

Jep, like any other "field=page.somefield" selector does fail with it. It's not the fault of "somefield" but InputfieldAutocomplete does not parse the "page.somefield" part, but just reads it as textvalue. I posted my changes to github so we can see what Ryan thinks of it. 

https://github.com/ryancramerdesign/ProcessWire/issues/1179

  • Like 2
Link to comment
Share on other sites

  • 4 years later...

Does anyone know if custom php code this still doesn't work with single page selection with autocomplete? From comments around here and last comment by Adrian on this github issue it would seem to be able to work but I just can't get my hook to do anything to the page selection.

Link to comment
Share on other sites

  • 1 year later...

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