2

I've seen similar questions but none seem to help.

I have two sections named jobs and jobDescriptions. I'm using the entries field to pull in a description field from the jobDescriptions section to the jobs entry. I need to output the related description field to the element api but I'm stuck. No idea where to go from here.

The other issue is that I also have a field in the jobs section that is named descriptionOverride. I want to show the descriptionOverride field if it has a value instead of the related field.

EDITED

<?php

use craft\elements\Entry;
use craft\helpers\UrlHelper;
return [
    'endpoints' => [
        'jobs.json' => function() {
            return array(
                'serializer' => 'jsonFeed',
                'elementType' => Entry::class,
                'criteria' => array('section' => 'jobs'),
                'transformer' => function(Entry $entry) {

// in a section call "jobDescriptions" and called through an entries field type in the "jobs" section entry
                    $jobDescriptionsEntry = $entry->jobDescriptions->one();
                    $relatedJobDescription = $jobDescriptionsEntry ? $jobDescriptionsEntry->jobDescription : '';

// in the section called "jobs" mentioned in the criteria above and used to override the relatedJobDescription if text is added to the field
                    $jobDescriptionOverride = $entry->jobDescription;
                    return array(
                        'title' => $entry->title,
                        'jobDescription' => ((string) $jobDescriptionOverride ?: $relatedJobDescription),
                    );
                },
                'meta' => array(
                    'description' => 'Jobs feed',
                ),
                'pretty' => true,
            );
        },
    ],
];
1

If relatedJobDescription is an Entries field, accessing the field from your jobs entry will not return the selected jobDescriptions entry directly. Instead, you will get an EntryQuery which you can use to get the selected jobDescriptions from the database. See Working with Entries Field Data and Querying Entries.

To get the jobDescriptions entry, you can use ElementQuery::one() (if your field allowed for multiple related jobDescriptions entries, you could use ElementQuery::all() instead).

Regarding the descriptionOverride, that's just a matter of checking if the override field contains some text and if it does, return it instead of the related job description.

Something like this should work:

// get the entry holding the related job description
$relatedJobDescriptionEntry = $entry->relatedJobDescription->one();
// handle an empty relatedJobDescriptions field gracefully
$relatedJobDescription = $relatedJobDescriptionEntry ? $relatedJobDescriptionEntry->description : '';
// get the description override from the current entry
$descriptionOverride = $entry->descriptionOverride;
return array(
    'title' => $entry->title,
    // return either the overridden description or the related job description
    'description' => ((string) $descriptionOverride) ?: $relatedJobDescription,
);

You can optimize this further to only retrieve the related job description if the override is not set.

3
  • Thanks for the reply and help. This got tabled for a couple months and not it priority. That's usually how it goes...
    – lanceb
    Jul 30 at 16:04
  • Well, I'm closer. The related job description is from a different section. Maybe that's the issue. It's returning null but I am getting the override job description if one is entered.
    – lanceb
    Jul 30 at 16:43
  • 1
    I got it. Works perfect. Now I have to apply it to several other fields. Fingers crossed.
    – lanceb
    Jul 30 at 17:19

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.