Jump to content

Custom selector relative to current page


suntrop
 Share

Recommended Posts

I've got some kind of blog setup with different topics and each should use its own set of categories/tags.

  • Home
  • Blog
    • Topic A
      • Categories
      • Tags
      • Articles
    • Topic B
      • Categories
      • Tags
      • Articles
    • Topic C
      • Categories
      • Tags
      • Articles

How do I have to write my custom selector for categories/tags to choose just from the items within the current topic? I can't use one of the defaults (parent page or specific template). I need to select the 2nd parent and find the category or tag template. But couldn't figure out, how to go up two times.

Link to comment
Share on other sites

Take a look at the example below "Custom PHP code to find selectable pages":

Example: return $page->parent->parent->children("name=locations")->first()->children();

Isn't that almost exactly what you need here? Of course you'd have to use ...->child("template=categories|tags")->children() or something like that.

Link to comment
Share on other sites

Well, I think I was to happy and didn't tested it completely. I noticed I can still select all tags, not only these saved underneath the parent parent.

I thought I have to deselect the "Parent of selectable pages" and after I did that, I get this error on all pages I want to edit:

Recoverable Fatal Error: Argument 1 passed to InputfieldPage::___findPagesCode() must be an instance of Page, none given (line 168 of /serverpath/wire/modules/Inputfield/InputfieldPage/InputfieldPage.module) 

I could go back and select the Homepage as parent, but after saving I still get the error message. Looks like I damaged something :-)

Link to comment
Share on other sites

I think that error means the selector did not find a page to return. This could be because it is hidden or unpublished or the selector is just wrong :-). I have re-read your post and not getting you clearly. Could you explain what you want to do using the pages in your tree (above) as examples? E.g. "I want to select the children of Topic A when I am editing the page Topic A"...i.e. be as specific as possible. 

Link to comment
Share on other sites

I deleted the selector via PhpMyAdmin and now I have access to my pages again.

Could you explain what you want to do using the pages in your tree (above) as examples? 

Let's say I am editing the article …

… /blog/topic-a/articles/entry/. On that page I only want to see tags saved in /blog/topic-a/tags/

… /blog/topic-b/articles/entry2/. On that page I only want to see tags saved in /blog/topic-b/tags/

I've attached a screenshot, that shows which tags are selectable and which tags are not.

select-tag.png

EDIT:

I have to use the tags field on the user template as well. It would be great if I can select and add from all tags for a user.

Edited by suntrop
Link to comment
Share on other sites

Seems I've found my selectors :-)

if ($page->template == 'admin') {
  // Find all tags, when editing users
  return $pages->find("template=tag");
} else {
  // Find tags only from current topic
  return $page->parent->parent->find("template=tag");
}
Link to comment
Share on other sites

In that case, Teppo's code should work. It works for me as follows..

return $page->parent->parent->child('name=tags')->children('');

..and in that page field, under "Input" -> template of selectable page(s) I have selected 'tag' (the template of all tags (A, B, C, D). This last bit is unnecessary; the selector still works but I'd keep it just to optimize the find...


Edit

I posted a second after or two after you did....Yep. that works too..but my selector won't bother with looking inside 'categories' or 'articles'; It will go straight for 'tags'....I think  :-)....not a big deal probably

Edited by kongondo
Link to comment
Share on other sites

I am guessing a fraction faster (I could be wrong) just because it is more specific....but in a real world scenario, especially in the backend, maybe it is not a big deal...But, doing it that way also reminds you what exactly you were looking for or in case there are multiple admins on the site, just to make it easier to know what's going on... :-)

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