2

I have content types Topics and Pamphlets. Pamphlets are related to topics (e.g. a pamphlet called 'Mass in B minor' could be related to the topic 'music') using 'relations' field-type:

pamphlet:
   ...
   relations:
      topics:
         multiple: false
   ...

What I want is to list all the Music pamphlets. More exactly, I want to be able to display each topic, with a list of pamphlets related to that topic.

There's a function record.related() but it works the other way, that is, I can use it when I display a pamphlet to show that the pamphlet is related to Music; but I can't use it when I display the Music topic to list all the pamphlets that are related to music.

(The documentation says that relations are always bi-directional, but I can't see how.)

Suggestions welcomed.

3 Answers3

2

I was simply wrong when I said that the related records() function didn't do the job. Here we are (nicked from the out-of-the-box record.twig template):

{% set relatedrecords = record.related() %}
    {% if relatedrecords is not empty %}
        <p> Examples:</p>
             <ul>
            {% for related in relatedrecords %}
             <li><a href="{{ related.link }}">
            {{ related.title }}</a></li>
            {%  endfor %}
             </ul>
    {% endif %}

In general, it seems, if A (a pamphlet) is related to B (a topic), exactly the same method can be used to show either the topic to which a given pamphlet belongs, or the pamphlets belonging to a given topic.

Apologies for getting this wrong, and congrats to the Bolt developers for a neat bit of work.

1

I now have an inefficient solution. Maybe someone can improve it.

To display all the pamphlets related to a given topic, I created a template topic.twig. Within that template, record.title contains the name of the topic (eg 'Music') and record.id contains the ID of the topic. We need to select pamphlets that are marked as related to a topic with that ID. The following code fetches all the pamphlets into pamphletlist then considers each one. Using the valuable dump() function, I found that the data structure for a pamphlet includes an array relation which includes an array topics. The code below works because in my data, one pamphlet can only be related to one topic. Code:

{#  to get a list of all pamphlets related to this topic #}
{% setcontent pamphletlist = 'pamphlets'  %}
{% for p in pamphletlist %}
{{ dump(p) }}
    {% for t in p.relation.topics %}
        {% if  record.id  ==  t  %} 
            <p><a href = " {{ p.link }} ">  {{ p.title }}  </a></p>
         {% endif %}
    {% endfor %}
{% endfor %}

It would surely be more efficient to use a where clause, something like this:

{% setcontent pamphletlist = 'pamphlets' where { relation.topics: ... }  %}

That would presumably get the election done at the database level, instead of peeking at each record in turn. But my experiments only produced twig syntax errors. Does anyone have a more efficient method?

  • Did you ever figure out a way to filter by content type? I'm trying to do something similar but looking at the dump of a related record my template fields contenttype is always null so not sure how this would work. – harri Nov 22 '18 at 15:22
  • {% set relatedrecords = record.related('pamphlets') %} I think would be the efficient way. – harri Nov 22 '18 at 15:34
0

Have you looked at the Related Content by Tags extension? This gives you a list of related content by tags: http://extensions.bolt.cm/view/45073af0-8585-4d5b-b978-fd6405858e0e

jadwigo
  • 339
  • 1
  • 9
  • Thanks Jadwigo but that uses part of the taxonomy system. I want to use a controlled list of Topics, so that the user-editor can occasionally add a new topic via the dashboard, which doesn't seem to be possible using the taxonomy system. Or am I wrong about that? – Thomas Green Jan 16 '15 at 08:42
  • The tags in the taxonomy system may be modified by the user (a user that is logged in in bolt) this is different to categories - categories are predefined in bolt. Still the website visitors can not edit tags – jadwigo Jan 25 '15 at 20:25