<p>If <code>relatedJobDescription</code> is an Entries field, accessing the field from your <code>jobs</code> entry will not return the selected <code>jobDescriptions</code> entry directly. Instead, you will get an <a href="https://docs.craftcms.com/api/v3/craft-elements-db-entryquery.html" rel="nofollow noreferrer">EntryQuery</a> which you can use to get the selected <code>jobDescriptions</code> from the database. See <a href="https://craftcms.com/docs/3.x/entries-fields.html#working-with-entries-field-data" rel="nofollow noreferrer">Working with Entries Field Data</a> and <a href="https://craftcms.com/docs/3.x/entries.html#querying-entries" rel="nofollow noreferrer">Querying Entries</a>.</p>
<p>To get the <code>jobDescriptions</code> entry, you can use <a href="https://docs.craftcms.com/api/v3/craft-elements-db-elementquery.html#method-one" rel="nofollow noreferrer">ElementQuery::one()</a> (if your field allowed for multiple related <code>jobDescriptions</code> entries, you could use <a href="https://docs.craftcms.com/api/v3/craft-elements-db-elementquery.html#method-all" rel="nofollow noreferrer">ElementQuery::all()</a> instead).</p>
<p>Regarding the <code>descriptionOverride</code>, 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.</p>
<p>Something like this should work:</p>
<pre class="lang-php prettyprint-override"><code>// get the entry holding the related job description
$relatedJobDescriptionEntry = $entry-&gt;relatedJobDescription-&gt;one();
// handle an empty relatedJobDescriptions field gracefully
$relatedJobDescription = $relatedJobDescriptionEntry ? $relatedJobDescriptionEntry-&gt;description : '';
// get the description override from the current entry
$descriptionOverride = $entry-&gt;descriptionOverride;
return array(
    'title' =&gt; $entry-&gt;title,
    // return either the overridden description or the related job description
    'description' =&gt; ((string) $descriptionOverride) ?: $relatedJobDescription,
);
</code></pre>
<p>You can optimize this further to only retrieve the related job description if the override is not set.</p>

CC BY-SA 4.0
2021-07-30T16:04:58.190Z
Thanks for the reply and help. This got tabled for a couple months and not it priority. That's usually how it goes...
lanceb
CC BY-SA 4.0
2021-07-30T16:43:28.327Z
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
CC BY-SA 4.0
2021-07-30T17:19:33.620Z
I got it. Works perfect. Now I have to apply it to several other fields. Fingers crossed.
lanceb
CC BY-SA 4.0
2021-04-08T07:38:54.583Z
2021-04-08T07:38:54.583Z
MoritzLost
<p>I've seen similar questions but none seem to help.</p>
<p>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.</p>
<p>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.</p>
<p>EDITED</p>
<pre><code>&lt;?php

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

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

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

<plugin-elementapi><entries-field><json>
Element API using the entries field to display related content from a separate section
# Element API using the entries field to display related content from a separate section

(Asked by: username_0 on 2021-04-07)

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
 [
        '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,
            );
        },
    ],
];

**Tags:** plugin-elementapi, entries-field, json


---

## 1 answers

### Answer by username_1 on 2021-04-08. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: Thanks for the reply and help. This got tabled for a couple months and not it priority. That's usually how it goes...  
> **username_0**: 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.  
> **username_0**: I got it. Works perfect. Now I have to apply it to several other fields. Fingers crossed.  

---

