Show a list of existing tags for new articles

When writing new blog articles I would like to see existing tags from older articles.
I have tried it with this code in the blueprint but it doesn’t work (nothing is showing up):

          tags:
            type: tags
            options: query
            query: site.index.pluck("tags", ",", true)
          taglist:
            type: structure
            default:
              - query: site.index.pluck("tags", ",", true)
            fields:
              tags:
                type: tags
                options: query
                query: site.index.pluck("tags", ",", true)

For existing articles, it is possible with this code to add more tags by using autocomplete but not with new articles.
How to show a list (structure) with all existing tags for new articles or at least being able to use the autocomplete in a tags input field?
Additionally it would be great to have independend page in the panel where it is possible to manage all the existing tags.

I cannot reproduce this issue. Are you sure your new articles use the same blueprint?

Sorry, I first wrote the question when I used

page.siblings.pluck("tags", ",", true)

which only worked for existing articles. Later I have changed that to the mentioned

site.index.pluck("tags", ",", true)

which solved the autocompletion problem with the input tags field.

But in the Structure field I don’t see a pre-populated list with existing tags:
image
I would like to have an overview of all existing tags so that I can choose easily which of them fit to the new article.

You mean because you set the default? But you can’t use query to set default values. Where did you find that documented?

What do you want to achieve here? Get as many entries as there are unique tags?

I didn’t find it somewhere, I just tried things out as I am new to Kirby.
Exactly, I just want to get an overview of all existing tags so that I can choose if some of them fit to a new article.
Something similar like this (from Ghost CMS):
image

Hm, but you would have to update this field all the time when new tags are created. It would probably make more sense to use an info field with a custom method that fetches and displays those tags.

When I try it with

          taglist:
            type: info
            text: "{{ site.index.pluck("tags", ",", true) }}"

I only see
image

That’s why I said you need a custom method that outputs a list of the tags, site.index.pluck("tags", ",", true) returns an array and you cannot echo an array.

So your method would loop through that array and return a string.

Even if I want to show it only in the Panel? I don’t need this for the public website.

Yes, because you cannot loop through this array in the Panel.

taglist:
  type: info
  text: "{{ page.yourCustomMethodThatReturnsTheRenderedString }}"

Inside your method, you can get as fancy as you like.

I tried it with a page method like this but couldn’t get it to work:

<?php

// fetch all tags
$taglist = $site->index()->pluck('tags', ',', true);

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'taglist' => function () {
            $data = json_encode($taglist) ;
                return $data;
        }
    ]
]);
?>

or this

<?php

// fetch all tags
$taglist = $site->index()->pluck('tags', ',', true);

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'taglist' => function () {
            foreach($taglists as $taglist) {
                return $this;
            }
        }
    ]
]);
?>

and this yaml

taglist:
            type: info
            text: "{{ {{ page.taglist }} }}"

$site is not defined in a plugin, use the site() helper instead.

Also, I would define $taglist inside the closure, not outside, otherwise the query is executed all the time.

This just shows a blue empty bar in the panel:

<?php

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'taglist' => function () {
            $taglist = site()->index()->pluck('tags', ',', true);
            foreach($taglists as $taglist) {
                return $this;
            }
        }
    ]
]);
?>

That doesn’t make sense, you return the page object. Whereas your method would have to return an string, e.g. an HTML list with a li element for each tag.

Like this?

<?php

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'taglist' => function () {
            $taglist = site()->index()->pluck('tags', ',', true);
            foreach($taglists as $taglist) {
                //return $this;
                echo '<li>'; print_r($taglist); echo '</li>';
            }
        }
    ]
]);
?>

List stays empty.

Not quite:

'taglist' => function () {
  $tags = site()->index()->pluck('tags', ',', true);
  $list = '<ul>';
  foreach($tags as $tag) {       
    $list .= '<li>' .  $tag . '</li>';
  }
  $list .= '</ul>';
  return $list;
}
1 Like

Thanks a lot but it is still empty.

          taglist:
            type: info
            text: "{{ page.taglist }}"

Then there’s something else wrong.

Is the plugin registered correctly?

Yes, now it works, I overwrote the ‘pageMethods’ part, thanks a lot!

<?php

Kirby::plugin('peleke/page-methods', [

  'pageMethods' => [
    'taglist' => function () {
        $tags = site()->index()->pluck('tags', ',', true);
        asort($tags);
        $list = '<ul>';
        foreach($tags as $tag) {       
          $list .= '<li>' .  $tag . '</li>';
        }
        $list .= '</ul>';
        return $list;
      }
    ]
]);
?>
1 Like