getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.59k stars 1.41k forks source link

Page paths and child collections #1967

Open Perlkonig opened 6 years ago

Perlkonig commented 6 years ago

I'm not getting any pick up on Discourse, so I thought I'd try here.

My table importer plugin creates a shortcode that pulls data from the page’s folder (where the .md file lives). It does this by looking at $this->grav['page']->path(). The problem is that when the page’s content is generated by the blog main page (as a preview), $this->grav['page']->path() points to the blog’s main page and not the individual post. The summary is pulled in as you would expect:

    {% for child in collection %}
      {% include 'partials/blog_item.html.twig' with {'page':child, 'truncate':true} %}
    {% endfor %}

But again, when the shortcode processes the child page, path() returns the parent page's path. How can I rectify this? I’ve been pouring over the API documentation, but nothing’s jumping out as the answer.

rhukster commented 6 years ago

that does seem odd, path() should be the physical path of the page object, that really shouldn't change.

Perlkonig commented 6 years ago

Thanks for looking into it. Linking with my plugin's issue: https://github.com/Perlkonig/grav-plugin-table-importer/issues/15.

rhukster commented 6 years ago

I've tested this and things are working as expected. I suspect that it's simply a case of you using $this->grav['page'] as this is the overall page that is rendering (per the route requested). I created a test case where i looped over a collection:

<h3>Manual Initiated Collection</h3>

{% set options = { items: {'@page.children': '/core/issues'}, 'limit': 5, 'order': {'by': 'date', 'dir': 'desc'}, 'pagination': true } %}
{% set collection3 = page.collection(options) %}

<ul>
{% for p in collection3 %}
<li>{{ p.title }} - {{ p.path() }}</li>
{% endfor %}
</ul>

And the path printed was the expected path of each page in the collection.

Perlkonig commented 6 years ago

OK, but in this case it's a shortcode. ShortcodeInterface doesn't have any capability of getting additional context. If $this->grav['page'] doesn't point to the actual page the shortcode is processing, what else can I use?

Perlkonig commented 6 years ago

No additional information?