3

I spent a fair amount of time yesterday searching for any other questions that may have been answered that are similar to this one, but haven't had much luck.

Essentially what I want to do is to daisy chain some Entry relationship fields. I'm not entirely sure if this is possible in Craft, as I haven't seen any documentation or commentary on it.

Here is how I would like to have my content organized. The site will have a few channels: States, Regions, Counties, Towns and Inductees. As you can tell, the relationships basically drill-down in an obvious way: States have Regions; Regions have Counties; while Counties have both Towns and Inductees. I like the idea of being able to add a new Town, select which County it belongs to, and automatically inherit the Region and State data. Ideally, I'd like to be able to do something in a Town template like {{entry.state.title}} which would pull the title of the state to which this town's county belongs.

Now, the States and Regions basically are just lists... The States page will be an archive listing all the Regions in the State. The Regions page will be an archive listing all the Counties in the Region. Would these be better suited as Categories, than Channels? If so, would there still be a way to access the State from, say, a Town or Inductee entry? I'd rather not have to rely on the content editor to have to select states and regions for each new Town/Inductee entry. I'd rather they only have to select its County.

I'm new to Craft, so I apologize if this question seems either absurd or incoherent :)

Thanks!

1 Answer 1

4

As usual with Craft you have several options. If you didn't need the index pages, I would suggest a location category, which you organized into 3 levels: states, regions, counties. Selecting a county location would also select its containing region and state. In your town entry you would do:

{% for l in entry.locations %}
  {{l.title}}
{% endfor %}

You would go through the loop three times, and {{l.title}} would be the state, region, and then county.

Disadvantages:

  • nothing in Craft enforces that you properly organize your location categories
  • nothing in Craft keeps you from choosing 3 states instead of one county for your town entry
  • the location index pages will be hard to write

Another option is to use a channel (or category) for states, a channel for regions, a channel for counties. The county entry would have a field for the containing region, the region entry would have a field for the containing state. And the town entry would have a field for the region. Then in the town template:

{{ entry.region.first.county.first.state.title }}

This will fail badly if any of the required relationships aren't there. You can test everything before you use it:

{% if entry.region.first %} ...

Or you can use the ?? (null-coalescing) operator:

{{ entry.region.first.county.first.state.title ?? 'no state' }}

Fetching the state title will be fairly expensive. You could use the preparse plugin to save the state name on the town entry.

For the index pages, you will want the regions that are related to the state, etc.

2
  • Amazing! Thank you so much for the detailed answer. Sep 2, 2016 at 19:28
  • Glad to help!!! Sep 2, 2016 at 19:29

Your Answer

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

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