Forum
Nested Related Collections
I have 2 collections:
- Opportunities
- Locations
My master Opportunities template utilises <perch:related>
to allow the user to select a location for the opportunity – works great.
I also have a region on the homepage that uses a template with <perch:related>
to allow the user to choose an opportunity to feature. However, as I also need to show the location as part of the featured opportunity, I end up needing nested <perch:related>
tags:
<perch:before>
<div class="home-featured-job">
<h3 class="pre-heading">New Opportunity</h3>
<h2>Featured Job</h2>
</perch:before>
<perch:related id="featuredJob" collection="Opportunities" label="Featured Job" max="1">
<div class="job-list-job-detail">
<h3><perch:content id="jobTitle" /></h3>
<span class="location">
<perch:related id="location" collection="Locations" label="Location" max="1">
<perch:content id="location" type="text" label="Location" required="true" title="true" />
</perch:related>
</span>
</div>
<perch:content type="textarea" id="jobDescription" words="30" append="..." />
<span class="job-list-type <perch:if id="type" value="Contract">contract</perch:if>">
<perch:content type="radio" id="type" label="Job Type" />
</span>
<a href="/opportunities/<perch:content type="slug" id="slug" />" class="btn btn-alt">View Job</a>
</perch:related>
<perch:after>
</div>
</perch:after>
This doesn't render anything for location on the homepage and also displays a 'location' field in the Perch admin for the homepage region, when all I need is the opportunity field.
Is this is even possible and if so, where am I going wrong?
Master opportunity template:
<div class="single-job-header">
<div class="job-list-job-icon">
<img src="/img/<perch:content id="icon" type="select" label="Icon" options="D365|d365, DNAV|dnav, DCRM|dcrm, E1|e1, ERP|erp, JDE|jde, IT|it" />.svg" alt="<perch:content id="icon" type="select" label="Icon" />">
</div>
<div class="single-job-title">
<h3 class="pre-heading">Selected Job Details</h3>
<h1><perch:content type="text" id="jobTitle" label="Job Title" title="true" order="2" /></h1>
</div>
<div class="apply-now">
<span class="job-ref">
Job Ref:
<perch:content type="text" id="ref" label="Job Ref." order="1" title="true" />
</span>
<a href="#apply-form" class="btn scroll-to-form">Apply Now</a>
</div>
</div>
<div class="job-meta">
<span class="job-meta-detail">
<perch:content type="radio" id="type" label="Job Type" options="Contract, Permanent" required="true" />
</span>
<span class="job-meta-detail">
<perch:related id="location" collection="Locations" label="Location" max="1">
<perch:content id="location" type="text" label="Location" required="true" title="true" />
</perch:related>
</span>
<span class="job-meta-detail">
£<perch:content type="text" id="annualFrom" label="From (£)" divider-before="Annual Salary" append="k" />
–
£<perch:content type="text" id="annualTo" label="To (£)" append="k" />
<perch:content type="checkbox" id="benefits" label="Benefits Included" />
<perch:content type="text" id="dayFrom" label="From (£)" divider-before="Day Rate" />
<perch:content type="text" id="dayTo" label="To (£)" />
</span>
</div>
<perch:content id="slug" type="slug" for="ref jobTitle" suppress="true" />
<div class="job-description">
<perch:content type="textarea" id="jobDescription" label="Job Description" editor="simplemde" markdown="true" divider-before="Job Overview" size="xl" />
</div>
<perch:content type="textarea" id="socialSummary" label="LinkedIn Summary" markdown="true" divider-before="LinkedIn" suppress="true" />
You can't nest relationships. You'll need to get your selections back from the homepage region and then display the collection filtered on that.
I thought that using
perch_content_create
and then returning the results of that region usingperch_content_custom
might provide the data I need to then render the collection based on that like so:However, this only returns:
Or is this not what you meant?
What's the ID you're looking for?
Well the slug would be fine as that's probably the easiest to filter on.
This seems to be working:
This is my
_home_featured_job_related.html
templateCan I give myself a point for solving it?
Is there a more elegant way to handle creating the region rather than an empty
perch:related
tag?What do you mean by creating the region?
Well at the moment I'm using
perch_content_create('Featured Job', array( 'template' => '_home_featured_job_related.html', ));
to create the region, but the template used is just emptyperch:related
tags. I could be overthinking it but it feels like there's an unnecessary step in there.How would you select your items without a region?