1

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.

<?php

use craft\elements\Entry;
use craft\helpers\UrlHelper;
use craft\elements\Tag;
return [
    'endpoints' => [
        'jobs.json' => function() {

            return array(
                'serializer' => 'jsonFeed',
                'elementType' => Entry::class,
                'criteria' => array('section' => 'jobs'),
                'transformer' => function(Entry $entry) {

                    return array(
                        'title' => $entry->title,

                        {# pulling the description field from the jobDescriptions section using the entries field relatedJobDescriptions in the jobs section. I'm obviously
way off base. #}
                        'description' => $entry->relatedJobDescription->description,
                    );
                },
                'meta' => array(
                    'description' => '',
                ),
                'pretty' => true,
            );
        },

        'jobs2.json' => function() {

            return [
                'serializer' => 'jsonFeed',
                'elementType' => Entry::class,
                'criteria' => ['section' => 'jobs'],
                'transformer' => function(Entry $entry) {

                    return [
                        'title' => $entry->title,
                    ];
                },
                'meta' => [
                    'description' => 'Recent news from Happy Lager',
                ],
                'pretty' => true,
            ];
        },
    ],
];
New contributor
lanceb is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
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.

Your Answer

lanceb is a new contributor. Be nice, and check out our Code of Conduct.

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.