Forum

Thread tagged as: Question, Problem, Runway

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" />
Toby Martin

Toby Martin 1 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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 using perch_content_custom might provide the data I need to then render the collection based on that like so:

perch_content_create('Featured Job', array(
            'template' => 'home_featured_job.html',
            ));

            $jobinfo = perch_content_custom('Featured Job',[
                'skip-template' => 'true'
            ]);

However, this only returns:

array(1) {
  [0]=>
  array(5) {
    ["_id"]=>
    string(2) "21"
    ["featuredJob"]=>
    array(1) {
      [0]=>
      string(1) "2"
    }
    ["_page"]=>
    string(1) "/"
    ["_pageID"]=>
    string(1) "1"
    ["_sortvalue"]=>
    string(4) "1000"
  }
}

Or is this not what you meant?

Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

perch_content_create('Featured Job', array(
            'template' => '_home_featured_job_related.html',
            ));

            $jobinfo = perch_content_custom('Featured Job', [
                'skip-template' => 'true'
            ]);

            $id = $jobinfo[0]['featuredJob'][0];

            perch_collection('Opportunities', [
                'filter'        => '_id',
                'match'         => 'eq',
                'value'         => $id,
                'template'  => '_home_featured_job.html'
            ]);

This is my _home_featured_job_related.html template

<perch:related id="featuredJob" collection="Opportunities" label="Featured Job" max="1"></perch:related>

Can 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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

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 empty perch:related tags. I could be overthinking it but it feels like there's an unnecessary step in there.

Drew McLellan

Drew McLellan 2638 points
Perch Support

How would you select your items without a region?