1

I have a grid field like this:

File - Text field - Relationship field

Where the file is a document, the text field is the title and the relationship field is the argument.

I would like to output the list of files like this:

Argument 1

  • File 1
  • File 2
  • File 3

Argument 2

  • File 4
  • File 5
  • File 6

And so on, therefore grouped by the relationship field.

Now I have something like

{grid}

<li><a href="{grid:file}"> {grid:text_field} </a> - {grid:relationship}{grid:relationship:title}{/grid:relationship}</li>

{/grid}

That shows me:

  • File 1 - Argument 1
  • File 2 - Argument 1
  • File 3 - Argument 1
  • File 4 - Argument 2
  • File 5 - Argument 2
  • File 6 - Argument 2

How can I output the items by grouping them by argument? It means that the relationship field must shows only one time per type, and create a list for every type.

4 Answers 4

1

I'm just thinking here, but if you install Stash and use something like the following. This code is not tested and not sure get_list can be nested but the theory should be sound :)

{exp:channel:entries url_title="{segment_2}"}
{exp:stash:set_list name="entries" parse_tags="yes"} //Grab all your entries
    {grid}
        {stash:title}{grid:text_field}{/stash:title}
        {stash:file}{grid:file}{/stash:file}
        {stash:related}{grid:relationship}{grid:relationship:title}{/grid:relationship}{/stash:related}
    {/grid}
{/exp:stash:set_list}

{exp:stash:set_list name="group_titles" unique="yes" trim="yes"} //Grab just the titles in a seperate list
    {grid}
        {stash:group}{grid:relationship}{grid:relationship:title}{/grid:relationship}{/stash:group}
    {/grid}
{/exp:stash:set}
{/exp:channel:entries}

{exp:stash:parse}
{exp:stash:get_list name="group_titles" prefix="outer"} //Grab the list of titles
    <h1>{outer:group}</h1>
    {exp:stash:get_list name="entries" match="#{outer:group}#" against="related" prefix="inner"} //Grab the entries and match them against the current group
    <li>{inner:title} / {inner:file} / {inner:group}</li>
    {/exp:stash:get_list}
{/exp:stash:get_list}
{/exp:stash:parse}
1

I resolved using Matrix and Playa. I used those because of the Playa parents tag. I could not use the native features of Grid and Relationship because, according to the documentation, “Currently it is not possible to get the parents of a relationship field that is inside of a Grid field”.

The code is:

{exp:channel:entries channel="arguments"}
 {title}
  {exp:playa:parents channel="documents" var_prefix="doc"}
   {doc:matrix_field search:relationship="[{entry_id}]"}
    <li><a href="{file}">{text_field}</a></li>
   {/doc:matrix_field}
  {/exp:playa:parents}
{/exp:channel:entries} 
1

I did a similar thing with matrix field and in a low variables field. Thanks to @stuartmcd69 - I simplified his a bit.

{exp:stash:set_list name="groups" parse_tags="yes" unique="yes" }
  {exp:low_variables:parse var="report_downloads" order_by="category"}
    {stash:group}{category}{/stash:group}
  {/exp:low_variables:parse}
{/exp:stash:set_list}

then further in the template the output

{exp:stash:get_list name="groups" unique="yes" trim="yes"}
  {exp:stash:embed name="downloads" stash:category="{group}"}
{/exp:stash:get_list}

and the embed template looks like

<h2>{stash:category}</h2>
<ul>
{exp:low_variables:parse var="report_downloads" search:category="={stash:category}"}
  <li>
    <a href="{file}">{title}</a>
  </li>
{/exp:low_variables:parse}
</ul>
0

Stephen T actually had a wonderful answer, maybe you try this:

{exp:channel:entries channel="zones"}
    <h4>{title}</h4>
    {parents field="zone_relationship_field" channel="club_listing"}
        {parents:title}<br>
    {/parents}
{/exp:channel:entries}

See this link Grouping entries by relationship field (title)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.