Collection item URL

  • Hello,


    One of the things I would love to see improve in v4 is how the Collection item URLs are managed. At the moment, we can only configure the search result/view page URL via the control panel. So apart from the search results template, we need to manage the URLs ourselves.


    One option would be to manually add it for each <a></a> link.


    HTML
    1. <a href="/team/<perch:content id="slug">"><perch:content id="name"></a>


    A single template may have the link in multiple places. A single item may be output by different templates in different contexts. So this option is not very practical. If you need to make a change, you'd have to change them all.


    Another option would be to use template includes:


    HTML
    1. <a href="<perch:template path="content/team/item_url.html">"><perch:content id="name"></a>


    This means we can manage the URL from a single template content/team/item_url.html. However, if we need to output that URL from inside a pair tag (categories, repeater, etc), we run into a scoping issue and we'd have to create another template content/team/item_url_scoped.html. This means we'd need to manage the URL in 2 templates and via the control panel for search results / view page URL.


    To overcome the scoping issue, we can use the each option to add the URL:


    PHP
    1. perch_collection('Team', [
    2. 'each' => function($item) {
    3.         // this can be managed from one place my_app_team_callback($item);
    4. $item['item_url'] = '/team/' . $item['slug'];
    5. return $item;
    6. }
    7. ]);


    However, we can't (easily) do this for related items inside perch:related tags.


    My point is, Runway does not really help with managing the URLs of Collection items. In comparison the Blog app lets you configure the post URL and makes it available to you in all contexts. So I think it would be great if the URL option in the control panel (or not) is made available in all contexts.




    The other thing I would love to see regarding the Collection item URLs is make the configuration more flexible. At the moment (for the search result/view page URL) we are very limited to what we can do. We can't format dates or use categories/relationships as far as I'm aware. Would it be possible to make the URL generation via a Perch template instead? This would give us a lot of flexibility:


    HTML
    1. <perch:if exists="category">
    2. /products/<perch:categories id="category" set="Products"><perch:if exists="perch_item_first"><perch:category id="catSlug"></perch:if></perch:categories>/<perch:content id="slug">
    3. <perch:else/>
    4. /product/<perch:content id="slug">
    5. </perch:if>
  • I'm currently sketching out a system whereby a collection can have a supporting PHP class defined to layer custom functionality on top. Immediately the plan is to add:


    • a templating callback (a bit like the each callback, but centrally defined)
    • event hooks
    • an indexing hook or insertion point

    These would support class inheritance as normal, so you could define some core functionality and then extend that for specific cases.


    I think this would probably give you the utility you needed, but whether or not a specific set of URL handling functionality might also be useful is still a valid question.