Issue with tag filtering via URL not working

Hello everyone,

I’ve just applied this Cookbook tutorial 1:1 to my project.

Now the issue:

  • The tag cloud is being shown correct on my page
  • When the Tag “Erziehung” is clicked, it generates a URL like http://eaa-mfr.test/inklusionsboerse/tag:Erziehung
  • But it still shows all articles on that page and not the articles with Tag “Erziehung” only

Do you maybe have the idea where I turned wrong?

Controller for that overview parent page:

<?php
return function ($page) {

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

  // fetch all tags
  $tags = $articles->pluck('tags', ',', true);

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

  // apply pagination
  $articles = $articles->paginate(10);
  $pagination = $articles->pagination();

  return compact('articles', 'tags', 'tag', 'pagination');
};

and the snippet with tagcloud

    <ul class="tags">
      <?php foreach ($tags as $tag) : ?>
        <li>
          <a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
            <?= html($tag) ?>
          </a>
        </li>
      <?php endforeach ?>
    </ul>

And the template?

Sorry, forgot it, this is simplified version of the template:

  <?php foreach ($items = $page->children()->listed()->paginate(10) as $item) : ?>
    <h2><a href="<?= $item->url() ?>"><?= $item->title() ?></a></h2>
    <?= $item->assetDescriptionCompactAnonymous()->kt() ?>
    <p><a href="<?= $item->url() ?>"><i class="fa-solid fa-chevron-right link--arrow"></i> Details</a></p>
  <?php endforeach ?>


  <ul class="tags">
    <?php foreach ($tags as $tag) : ?>
      <li>
        <a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
          <?= html($tag) ?>
        </a>
      </li>
    <?php endforeach ?>
    </ul>

Exactly what I thought. Instead of using your $articles variable from the controller, you define the list in your template as $items again.

Now i renamed articles into items in my controller and the issue is the same :see_no_evil:

Controller now looks like this:

<?php
return function ($page) {

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

  // fetch all tags
  $tags = $items->pluck('tags', ',', true);

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

  // apply pagination
  $items = $items->paginate(10);
  $pagination = $items->pagination();

  return compact('items', 'tags', 'tag', 'pagination');
};

If you still redefine your variable in your template, it doesn’t help.

It’s as if you did:

$a = 5;
$a = 7;

And then wonder why $a is not 5.

Damn now everything clicked to me and now everything works as intended!

A last question though, what would be most elegant way to reset the current view back to all elements?

A link “All” that links to the main page url.

1 Like