URL problem with tag filtering


#1

Maybe I am the only one but on my side I have a problem with the url tags contain a space.
Example for the tag “website design”, the URLs do not work:

— projects/tag:website+design

or

— projects/tag:website%20design

An idea of the problem ?
I looked at the doc (https://getkirby.com/docs/cookbook/content-structure/filtering-with-tags) but maybe I missed some things…


#2

Use urlencode() and urldecode() when working with spaces.


#3

Does not work for my code.
(I specify that I speak of the page “project” and not of the parent page “projects”.)
“Projects” being the homepage.

My code on project.php:

<?php foreach(str::split($page->tags()) as $tag): ?>
<a href="<?= url('tag:' . urlencode($tag)) ?>"><?= $tag; ?></a>
<?php endforeach ?>

#4

And the code where the filtering takes place?


#5

My code is the same as this one:
https://getkirby.com/docs/cookbook/content-structure/filtering-with-tags

Controller of projects.php template:

<?php

return function($page) {

  // fetch the basic set of pages
  $projects = $page->children()->listed()->flip();

  // add the tag filter
  if($tag = param('tag')) {
    $projects = $projects->filterBy('tags', $tag, ',');
  }

  // fetch all tags
  $tags = $projects->pluck('tags', ',', false);

  // apply pagination
  $projects   = $projects->paginate(9);
  $pagination = $projects->pagination();

  return compact('projects', 'tags', 'tag', 'pagination');

};

I just added this code in my project.php template:

<?php foreach(str::split($page->tags()) as $tag): ?>
<a href="<?= url('tag:' . urlencode($tag)) ?>"><?= $tag; ?></a>
<?php endforeach ?>

It works great on a version of Kirby 2


#6

Hmm ok ok
I added the code but it would be nice to put it in the documentation…
Why does it work with Kirby 2 ?

Changed:

if($tag = param('tag')) {

on:

if($tag = urldecode(param('tag'))) {

#7

It didn’t work with Kirby 2 with spaces (or other funny characters) without the urlencode/decode stuff… and therefore this forum has multiple question regarding spaces in tags…


#8

I just ran into this myself… by bringing over working code from K2. It worked for me too with K2 without the urldecode.


#9

Maybe you used the tagcloud plugin?


#10

Nope. I did it like the cookbook (for Kirby 2) said. I did have a tag cloud on the site, but I wrote it by hand without a plugin. Weird. I just lifted the same code into the Kirby 3 version of my site and found this issue.


#11

:open_mouth: You were right! It doesn’t work. I just played close attention to my website (thats been online for about 2 years) and it does indeed not filter with a space in the tag. It didn’t break or throw a whoops or anything though… but i just double checked the list of matches it returned and it brings back all of them.

Of course you were right @texnixe