If use the following code:

$currentEntry = craft()->entries->getEntryById($entryId);
$currentContent = $currentEntry->getContent()->getAttributes();
$currentAttributes = array();

$fieldLayout = $currentEntry->getFieldLayout();
foreach ($fieldLayout->getFields() as $fieldLayoutField) {
    $field = $fieldLayoutField->getField();
    if (isset($currentContent[$field->handle])) {
        $currentAttributes[$field->handle] = $currentContent[$field->handle];

It retrieves all data that I want, but I don't get the data from relation fields. If an asset or entry has been selected, I won't receive that data. The result from that field is always null.

Does anyone know how to do this?


If you already know the custom field name then it is pretty straight forward:

$currentEntry = craft()->entries->getEntryById($id);
$relatedEntries = $currentEntry->customFieldName;

Then you could either loop through each one:

foreach ($relatedEntries as $entry) {
    echo $entry->title;

Or simply get the data from the first one:

echo $relatedEntries->first()->title;

Remember that all related entries come back as an array even if you have specified that the user can only select 1.

Hope this helps

  • It was not exactly what I was looking for, but it got me in the right direction.
    – Hubert
    Aug 8 '14 at 19:42

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.