Encoding/Decoding Tag URLs

I’m stuck on something where I may be missing something simple.

I’m trying to get my tag URLs to encode/decode properly to avoid spaces in the URLs for tags that have more than one word. I’m following the solution in this forum post.

My code:

blog.php controller:

<?php

return function ($page) {
  $blog = collection("blog");
  
  $byYear = function($p) {
    return $p->date()->toDate("Y");
  };
  
  $tag = urldecode(param("tag"));
  if (empty($tag) === false) {
      $blog = $blog->filterBy("tags", $tag, ",");
  }

  return [
    "tag"   => $tag,
    "blog" => $blog->sortBy("date", "desc")->paginate(10)->group($byYear)
  ];
};

article.php template:

<p class="meta tags">
  <?php if (!empty($tags)): ?>
	Tagged:
	<ul class="tag-list">
	  <?php foreach ($tags as $tag): ?>
	    <li>
	      <a class="tag p-category" href="<?= $page->parent()->url(["params" => ["tag" => urlencode($tag)]]) ?>">
	        <?= esc($tag) ?><?php if (next($tags)) : echo ","; endif ?>
	      </a>
	    </li>
	  <?php endforeach ?>
	</ul>
  <?php endif ?>
</p>

It keeps returning URLs with: http://example.com/blog/tag:Tag%2BOne.

What am I missing?

1 Like

To convert spaces directly during the input, you can add a slug filter to the input field. Then your tags are already saved without spaces and you avoid additional work later. At the same time umlauts will be converted.

Question 1: Are you interested in managing the tags in a central place?
Question 2: Do you want the tags to remain dynamic or do you want to predefine the tags with a predefined list to avoid duplications?

Yes, but I don’t really understand the problem When such a parameter is decoded again, you get the Tag One again, which should be the value stored in your page, and it should get filtered correctly? Will also work with umlauts.

The slug field is not really a replacement for a tags field (purpose of tags field is to add multiple tags, not just one as in the slug field). Also, you cannot un-sluggify words…

Ah, maybe I’m misunderstanding then. I thought using urldecode/urlendcode would help me get rid of the %2B in the URLs and make it a +?

Hence my two further questions to better understand his usage of tags. This is not clear from the question for me.
I use tags in a central place and find this solution perfect. Maybe this solution is also interesting for him.

Yes, I remember having seen them with a + in the past as well. But anyway, I think the syntax

 $page->parent()->url(["params" => ["tag" => $tag]]) 

already rawurlencodes the tags without having to use urlencode()

Are you interested in managing the tags in a central place?

I’m not doing anything fancy. Following a similar setup to the Starterkit, showing tags on article pages and filtering them via the blog template.

Do you want the tags to remain dynamic or do you want to predefine the tags with a predefined list to avoid duplications?

I’d want to avoid duplication.

already rawurlencodes the tags without having to use urlencode()

Oh, interesting. It still showing up with the other way in the URLs. Maybe it’s my local server setup or something?

Yeah, in my environment as well, but works fine and that’s important after all?

I use this solution on a customer site. The topic are job offers.

On site.yml you can enter the tags in a structure field and manage them centrally.

… and select them on the blog page with a multiselct field. Or on any other page.

The tags are then applied in a filter.

If this is interesting for you, I’ll add the solution here with the corresponding code.

1 Like

Yeah, it does work, just doesn’t look that great. :sweat_smile: Maybe I’ll explore routes if I can’t get it to change to the cleaner + in this case.

Okay, my fault on the confusion! I realized after looking at this again more closely that %20 is the encoding for space so the %2B is working, as that’s the encoding for a +. I misunderstood and thought it would make a + in the URL. But’s that’s not how it works. :upside_down_face: Thanks for the help!