Jump to content

View multiple tags at any one time, rather than just one...


Go to solution Solved by LostKobrakai,

Recommended Posts

Hi folks,

I have some tags setup, using the Page field options, which is really flexible and works great. However, I am curious to know if it's possible to view multiple tags at any one time? Is this possible?

For example, I know to create a tag page (for example, www.domain.com/tags/events), you do the following:

<?php $tags = $pages->find('parent=/projects/, tags=' . $page . ', sort=sort'); ?>

Is it possible for a query to exist so you can view multiple tags? www.domain.com/tags/events&films&outdoor

Any thoughts? Good, bad?

Cheers!

R

Link to post
Share on other sites
  • Solution

You can use GET vars to do so:

<?php

// URL: www.domain.com/tags/?tags=events,films,outdoor
//
// Alt. URL with different template than tags overview: 
// www.domain.com/tags/multiple/?tags=events,films,outdoor

$tags = $sanitizer->selectorValue($input->get->tags);

$taggedPages = $pages->find("…, tags=$tags");
  • Like 1
Link to post
Share on other sites
  • 1 month later...

Thanks again for your solution. I'm wondering if I can further enhance this more?

See my screenshot: http://d.pr/i/18c1N

Is it possible to create a submit so if the user selected architecture from the 'Show me' list and 'Education' from the 'For' list (which are two separate Page Fields) it would send them to a URL showing items with both sets applied? Is there any documentation available on something like this? Is it common?

Thanks in advance.

Link to post
Share on other sites

<?php

// URL: www.domain.com/tags/?showme=events,films,outdoor&for=education

//

// Alt. URL with different template than tags overview:

// www.domain.com/tags/multiple/?tags=events,films,outdoor

$showme = $sanitizer->selectorValue($input->get->showme);

$for = $sanitizer->selectorValue($input->get->for);

$taggedPages = $pages->find("…, showme=$showme, for=$for");

  • Like 2
Link to post
Share on other sites

Thanks for this help. I can work out how I would return this as a page (so have this in my template) but I'm unsure how it would work having this as a filter with a submit button? Would it be as simple as front end JS to use the values to create a URL? Or would there need to be server side code? If someone selects the two filters and presses 'Go' how would it know which URL to go to? Using JS I am assuming?

Link to post
Share on other sites

It could be done with or without JS, input fields in HTML that are part of a form object will be send as the values in the URL specified in the action attribute, which can be the same page from when you are sending the data. So how do you handle this vars in the server if there is data in the POST variables? In the same template used by your page with the form, you can have something like this:

if($input->post){
    echo $input->post->fieldName; 
    //Etc
}

I learned a lot about forms in PW from this post from Soma.

Link to post
Share on other sites

Thanks @LostKobrakai.

I have come up with the following:

<?php $sectors = $sanitizer->selectorValue($input->get->project_sectors); ?>
<?php $tags = $sanitizer->selectorValue($input->get->project_tags); ?>

<?php $results = $pages->find('parent=/project-library/, sort=sort, project_sectors=$sectors, project_tags=$tags'); ?>

<?php foreach ($results as $result) : ?>
    <h1><?php echo $result->title; ?></h1>
<?php endforeach; ?>

However, I'm unsure what the $input should be changed to? The URL, for example, could be: www.domain.com/project-library-results/?project_sectors=architecture&project_tags=education

So, am I right in thinking that based on what the user selects, it will build a URL using JS, then on submit, take you to this page and the $input will find the relevant items from the URL and build the result based on this?

Link to post
Share on other sites

Ah yes, okay. So essentially this is very similar to the search setup on PW. But instead the action in the form would be a custom URL based on what they have selected, rather than a specific template.

Link to post
Share on other sites

This is my full setup.

The idea is that the input value will be updated per click via JS, so they click 'Landscape Design' it'll update the value to 'landscape design' etc.

<form class="project-library-filter" action="<?php echo $pages->get('template=project-library-results')->url; ?>" method="get">
    <input type="text" name="project_sectors" value="architecture,urban-design" />
    <button type="submit" name="submit">Go</button>
</form>

On my template page for the results the following error is shown:

Error: Exception: Unknown Selector operator: '=$' -- was your selector value properly escaped? (in /Users/rich/Sites/Freelance/levitt-bernstein/wire/core/Selectors.php line 281)

<?php $sectors = $sanitizer->selectorValue($input->get->project_sectors); ?>
<?php $tags = $sanitizer->selectorValue($input->get->project_tags); ?>

<?php $results = $pages->find('parent=/project-library/, sort=sort, project_sectors=$sectors, project_tags=$tags'); ?>

<?php foreach ($results as $result) : ?>
    <h1><?php echo $result->title; ?></h1>
<?php endforeach; ?>

Any thoughts?

Link to post
Share on other sites

I meant to ask; if the user selects 'All', is there a value I can use for my project_sectors and/or project_tags that return them all?

<?php $results = $pages->find("parent=/project-library/, sort=sort, project_sectors=$sectors, project_tags=$tags"); ?>
Link to post
Share on other sites

Not for selectors per se, but you can use this:

<?php
// Get url data
$sectors = $sanitizer->selectorValue($input->get->project_sectors);
$tags = $sanitizer->selectorValue($input->get->project_tags);
// Build Selector
$selector = "parent=/project-library/, sort=sort";
if($sectors != "all") $selector .= ", project_sectors=$sectors";
if($tags != "all") $selector .= ", project_tags=$tags"; 
// Find pages
$results = $pages->find($selector);
?>

<?php foreach ($results as $result) : ?>
    <h1><?php echo $result->title; ?></h1>
<?php endforeach; ?>
Link to post
Share on other sites

Anyone else with any idea why commas don't work in the URL? For example, this URL returns no results:

http://localhost/Freelance/levitt-bernstein/project-library-results/?project_sectors=architecture,urban-design&project_tags=all&submit=

but if I do each of the project_sectors separately:

http://localhost/Freelance/levitt-bernstein/project-library-results/?project_sectors=architecture&project_tags=all&submit=

http://localhost/Freelance/levitt-bernstein/project-library-results/?project_sectors=urban-design&project_tags=all&submit=

They both return the same result...

If I output $sectors with just one it results as architecture but if I do it when I have two it wraps it in quotes "architecture,urban-design" which I don't know is of any help?

Furthermore, I think we have to use | rather than a comma to separate our values, so instead of $sectors outputting "architecture,urban-design" it would output architecture|urban-design, yes? However, | gets removed when it is sanitised so I'd have to remove that?

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      Displays image tags overlaid on the thumbnail using customisable colours. This makes it easier to see which images have which tags without needing to open the edit pane for individual images or changing to the list view.
      Screenshot

      Usage
      Enable tags for one or more image fields. Install the Image Thumbnail Tags module. Optionally configure colours for any of your tags.
       
      https://github.com/Toutouwai/ImageThumbnailTags
      https://modules.processwire.com/modules/image-thumbnail-tags/
    • By Andi
      Continuing my journey into PW hooks, I'm trying to find a way to retrieve all images from a page that explicitly *do not* have a certain tag (or tags) attached to them.
      Found this post from 2015
      But I'm wondering if there's a more elegant way to go about this.
      Let's say I have a multi-image field called "images_header" and instead of
      $page->images_header->findTag('mytag'); I would like to do this:
      $page->images_header->excludeTag('mytag'); So I'd be able to do
      // find images that don't have the tag "mytag" $images = $page->images_header->excludeTag('mytag'); // check if there's any images if (count($images)>0) { // do something.. } Would this be possible by hooking into Pagefiles somehow?
      There's this bit in /wire/core/Pagefiles.php Line 626 that I'd basically just need to reverse (or at least in my mind 😄 )
      public function findTag($tag) { $items = $this->makeNew(); foreach($this as $pagefile) { if($pagefile->hasTag($tag)) $items->add($pagefile); } return $items; } Any ideas on how this could be done in a graceful manner?
      Thanks in advance!
    • By Sanyaissues
      After enable the Tags File Compiler module, echoing variables with brackets syntax doesn't work. I try something simple like {page.title} and is displayed like the tag isn't parsed:
      {page.title} This problem happens with PW 3.0.108, i have another installation with 3.0.82 and it works. Any Ideas? thanks.
    • By sww
      Hey there,
      is there really no way to turn the OR logic into an AND logic when selecting pages by (e.g.) tags?
      so instead of
       $pages->find("template=exhibitions, tags=foo|bar") something like
       $pages->find("template=exhibitions, tags=foo&&bar") so the pages needs to have all requested tags, not just any of them.
       
      Thanks,
      Stefan
    • By dimitrios
      Hello,
      I have created a field of type Page Reference and input field type Page Auto Complete, so that users of role 'writer' can add new tags to their articles. However, only a superuser can add new tags through the field, even though 'writer' roles have the permission to create  pages of template 'tag', and the permission to add children in the parent template. New tags in the Page Tree can be added normally. Is there something I am missing?

×
×
  • Create New...