Jump to content

How to use find() on child-array?


n0sleeves
 Share

Recommended Posts

I am having trouble outputting my tagging system correctly. Each music track has a Multi-Page-List-Select field-type which tags associated categories.

Tags is a parent page with nested categories beneath. Genre, Mood, Drum Tags each contain additional categories which are ultimately the tags I am associating on the music tracks. 

Screenshot-01162015-115618AM.jpg

 In the code below. $genres is where I am having trouble.  I think my selector is correct, but my chaining is wrong? 

 
// Find Songs
$tracks = $pages->get("/Music/")->find("template=track, parent.title!=Drums");

  foreach ($tracks as $track) {
      $track_html  = "<li>";
      $track_html .= "<a>{$track->title} </a>";
      
      // FIND GENRES EACH TRACK BELONGS TO. 
      $genres = $track->tags->find("parent=genre");
      
      // OUTPUT ONLY GENRES 
      foreach ($genres as $genre) {
        $track_html .= $genre->title . " "; 
      }
      
      $track_html .="</li>";
      echo $track_html;
  }

 

Thank you 

Link to comment
Share on other sites

Interesting approach to tagging :)

You’re trying to specify the parent by name, but I’m pretty sure ProcessWire would much rather have a page ID or a path (meaning, wrapped with forward slashes).

Because PW can tell “genre” is not numeric (so not an ID), it treats it like a path, as if the slashes were there, but there are no tags under “/genre/”. They’re under “/tags/genre/”. So try that and it should work. Alternatively, specify that you’re selecting by name:

$genres = $track->tags->find("parent.name=genre");

I can see how this can seem confusing, because when if you were looking for a direct child of the homepage, it would work. In that case, the path is the same as the name with slashes.

Because you’re not really selecting “dynamic” pages here, but rather pages that are a fixed part of your site’s structure, you might want to use IDs instead. That way, if you later want to change a page name/path, you can do it comfortably in the admin, and don’t have to go around fixing your selectors.

  • Like 1
Link to comment
Share on other sites

You’re trying to specify the parent by name, but I’m pretty sure ProcessWire would much rather have a page ID or a path (meaning, wrapped with forward slashes).

Because PW can tell “genre” is not numeric (so not an ID), it treats it like a path, as if the slashes were there, but there are no tags under “/genre/”. They’re under “/tags/genre/”. So try that and it should work. Alternatively, specify that you’re selecting by name:

$genres = $track->tags->find("parent.name=genre");

I can see how this can seem confusing, because when if you were looking for a direct child of the homepage, it would work. In that case, the path is the same as the name with slashes.

Because you’re not really selecting “dynamic” pages here, but rather pages that are a fixed part of your site’s structure, you might want to use IDs instead. That way, if you later want to change a page name/path, you can do it comfortably in the admin, and don’t have to go around fixing your selectors.

Providing the full parent path did not work. However, providing either  "parent=ID" or "parent.name=genre" did the trick! :)   BTW, great tip / idea on using ID's in case I ever need to change path or names. 

Thank you so much for the solution! 

Link to comment
Share on other sites

Not directly related to your question, but if your tag list is getting too long, it might be useful to activate the "Page Auto Complete" module from Core, which allows you to type in the tags that you want to add, instead of clicking through the list. I find this much easier when I have long lists of tags.

Using this module, an approach that I often use for tagging is the following:

  • Create a "tag" template with only a title field
  • Create a "Tags" page under which you will put all the tags
  • Create a "tags" Page field with the following options:
    • Multiple pages allowed
    • Parent of selectable pages: Select the "Tags" page created previously
    • Template of selectable pages: Select the "tag" template
    • Input field type: Select "PageAutoComplete"
    • Check the "Allow new pages to be created from field" option

Now, when entering tags, you can type in the name, if it exists already you an select it, otherwise press "Enter" and the tag will be created automatically.

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