0

I need to generate a unique id for Playa child element so that an accordion works properly. For example:

{exp:playa:children field="doctors_locations"}
    <a href="#id-{count}-{url_title}" class="accordion-toggle">{locations_displayed_name}</a>
    <div id="id-{count}-{url_title}">...</div>
{/exp:playa:children}

The {count} tag gives me the row count of the Playa object. This results in:

<a href="#id-1-my-hospital-url-title" class="accordion-toggle">My Hospital Name</a>
<div id="id-1-my-hospital-url-title">...</div>

This Playa relationship is Doctors > Locations - a one-to-many relationship. So, if the same location belongs to multiple doctors, I get duplicate ids on a list page.

In order to get a unique ID for each entry on this page, what I really need is the url_title of the parent in this relationship. This is what I'm looking for:

<a href="#id-1-my-doctor-url-title" class="accordion-toggle">My Hospital Name</a>
<div id="id-1-my-doctor-url-title">...</div>

Again, this is a one-to-many (one doctor works at many locations), so I will get a unique ID for each if I can figure this out.

0

You could use the var_prefix parameter which is designed to help you avoid naming conflicts with variables in your template.

{exp:channel:entries channel="doctors"}
<!-- 
-- url_title = to parent url 
-- and 
-- loc:url_title = playa entry url_title
-->
  {exp:playa:children field="doctors_locations" var_prefix="loc"}
    <a href="#id-{loc:count}-{url_title}" class="accordion-toggle"> 
     {loc:locations_displayed_name}
    </a>
    <div id="id-{loc:count}-{url_title}">...</div>
  {/exp:playa:children}
{/exp:channel:entries

You will need to edit your template to prefix any of your playa children custom fields with loc: for them to be parsed correctly.

1
  • This will not work as it still generates the same ID for Playa entries that are the same. I'm showing a list of doctors, and for each doctor a Playa related hospital. Two doctors can work at the same hospital, and if same hospital is displayed multiple times on the same page, the accordion toggle for that hospital gets the same ID. <a href="#id-1-hospital-name" class="accordion-toggle location-name">Hospital Name</a>. I initially thought your solution would work, but alas it does not. I need to generate a unique ID for a hospital displayed twice on the same page. Make sense? – Vaughn D. Taylor Mar 26 '15 at 14:58
0

Ok, got this figured out. You can embed a call to a Playa parent within the Playa child. I'm still using the var_prefix to avoid collisions...

{exp:playa:children field="doctors_locations" var_prefix="loc"}
    <li class="accordion-navigation{if {loc:count} == 1} active{/if}">
        <a href="#id-{loc:count}-{exp:playa:parents limit='1'}{url_title}{/exp:playa:parents}" class="accordion-toggle location-name">{loc:locations_displayed_name}</a>
        <div id="id-{loc:count}-{exp:playa:parents limit='1'}{url_title}{/exp:playa:parents}" class="content{if {loc:count} == 1} active{/if}">...</div>
    </li>
{/exp:playa:children}

You need to make sure you set the limit='1' so the parent url_title does not repeat for every iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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