getgrav / grav-plugin-taxonomylist

Grav TaxonomyList Plugin
https://getgrav.org
MIT License
25 stars 14 forks source link

children_only being set to true doesn't honor base_url but only gets tags of children of page TWIG code is in #38

Open fseesink opened 3 years ago

fseesink commented 3 years ago

It is very possible I misunderstand the intention and that this is by design, but it seems like the children_only setting is not taking into account the base_url setting. Instead, it is only taking tags from the children pages of the page where this code is being loaded.

As an example, I am using Grav with Gantry5's Helium theme and was trying to create a sidebar on both the blog_list as well as individual blog_item pages that would list the popular tags from all the blog posts (but only those pages and not all the pages on the site). So I thought that by using code such as the following, this would be achieved:

<h4>Popular Tags</h4>
<table>
  <tr>
    <td>
{% include 'partials/taxonomylist.html.twig' with {base_url: '/blog', taxonomy: 'tag', children_only: true} %}
    </td>
  </tr>
</table>
<p class="nomarginbottom"></p>

Now this works perfectly on the blog_list page that is at /blog. But the moment you load any blog_item pages, this code loads up an empty list. This makes sense if it is assuming children of the blog_item page, but that seems contrary to the idea. I would think the 2 parameters define what pages the plugin checks for tags. But again, it could be I misunderstand this.

Is this the actual design? And if so, is there a way to achieve what I am trying to do? Because I don't see a way to do so.

fseesink commented 3 years ago

To be clear, if I do NOT put in children_only: true then the taxonomy list includes tags from ALL pages on the site, even those outside of /blog. Re-reading the docs I see that it says (see bold/italized lines):


...

Simple Include

The plugin provides a Twig template that you need to include in your theme. Something like:

{% include 'partials/taxonomylist.html.twig' with {base_url: my_url, taxonomy: 'tag'} %}

Where my_url is the URL to link to where the collection can be filtered (e.g. /blog) and the taxonomy points to a specific taxonomy type to display (e.g. tag). This will display all tags throughout your site

Child-only Include

You can also include pass an optional parameter that will show taxonomy for child-pages only:

{% include 'partials/taxonomylist.html.twig' with {base_url: my_url, taxonomy: 'tag', children_only: true} %}

...


So the first bolded line seems to indicate that doing so--displaying all tags throughout the site--IS the intended behavior if you do NOT specify/set children_only: true. But what is not clear is whether setting that should limit the tags to just those from pages under the base_url (which to me seemed logical) or those only from pages below the specific page in which the TWIG code runs. The latter I find strange, as what would be the purpose?

flow7 commented 1 year ago

I have the exact same problem - it seems I can't meaningfully combine the two factors, right?

Following scenario:

Desired outcome:

Any suggestions on how to set this up?

pmoreno-rodriguez commented 8 months ago

Hi @flow7 In your scenario, in sidebar you have to write this code:

    {# Taxonomy list (if taxonomylist plugin is enabled) #}
        {% include 'partials/sidebar/taxonomylist.html.twig' 
        with {'base_url':new_base_url, 'taxonomy':'tag', children_only: true} %}

And in your taxonomylist.html.twig this:

{% set taxlist = children_only is defined ? taxonomylist.getChildPagesTags() : taxonomylist.get() %}
{# If there is a taxonomy, load taxonomylist.html.twig #}
{% if taxlist and page.template == 'blog' %}
<section>
    <header>
        <h2>Popular Tags</h2>
    </header>
    <ul class="actions">
        {% for tax,value in taxlist[taxonomy] %}
            {% set label_class = uri.param(taxonomy) == tax ? 'secondary' : 'primary' %}
            <li>
                <a class="button small secondary {{ label_class }}" href="{{ base_url }}/{{ taxonomy }}{{ config.system.param_sep }}{{ tax }}">{{ tax }}</a>
            </li>
        {% endfor %}
    </ul>
</section>
{% endif %}