1

I am relatively new to Craft CMS and I have encountered an issue that I have attempted to debug for quite some time.

When using the {% dd %} function to dump my array, a load of my custom fields display as 'null' apart from the plain text fields.

The custom field that I am looking at returning is 'entryThumbnail'

Below is the dump of array data:

'customFields' => craft\behaviors\CustomFieldBehavior#2
    (
        [owner] => craft\elements\Entry#1(...)
        [yii\base\Behavior:_attachedEvents] => []
        [hasMethods] => false
        [canSetProperties] => false
        [contentBlockOne] => null
        [contentBuilder] => null
        [entryHeader] => null
        [entryThumbnail] => null
        [imageSlider] => null
        [menus] => null
        [navigation] => null
        [pageVideo] => null
        [seoDescription] => 'TEST'
        [seoImage] => null
        [seoKeywords] => 'ANOTHER TEST'
        [seoTitle] => 'TITLE TEST'
        [destinationUrl] => null
        [destinationEntry] => null
        [label] => null
        [menuLabel] => null
        [menuEntries] => null
        [slideImage] => null
        [slideTitle] => null
        [slideCaption] => null
        [blockBackgroundColour] => null
        [blockConfiguration] => null
        [textBlock] => null
        [imageBlock] => null
        [entrySubMenu] => null
        [entryDestination] => null
        [craft\behaviors\CustomFieldBehavior:_customFieldValues] => []
    )

Here is my code that I have written (Looping through a matrix field with the handle 'entrySubMenu') and then attempting to return custom field data of an entry field in the matrix.

{% set menuItems = entry.entrySubMenu.all() %} 

{% if menuItems|length %} 

        {% for menuEntry in menuItems %}

            {% set menuEntryItem = menuEntry.entryDestination.one() %}

            {% set entryItem = craft.entries().id(menuEntryItem.id).one() %}

            {% dd entryItem %}

        {% endfor %}

{% endif %}

1 Answer 1

1

We may need a bit more info to properly debug this, but I wanted to offer some advice and context surrounding the structure of the loop and fetching data so we can at least make sure we're examining the right content!

My suspicion is that this is just a limitation of Craft or PHP's serialization capability, and it's discarding field values that are prepared natively as ElementQuery objects. For performance reasons, Craft doesn't attempt to automatically load every relationship you've defined on an Entry (or Asset, or Matrix Block, etc)—instead, your relational fields are presented as a set of criteria for fetching the related content, and you're responsible for calling .one() or .all() to execute those queries.

When I dump an Entry object, I to see null values for my relational fields. 👍

In your example (as best I can tell), entryItem and menuEntryItem will end up being the same Entry object, just loaded twice in different ways. Check out this alternative means of loading the data:

{# Load Menu Item matrix blocks (replacing `myBlockTypeHandle` with your block type's handle), and "eager-load" the destination Entries (`entryDestination`) and their thumbnail Asset (`entryThumbnail`): #}
{% set menuItems = entry.entrySubMenu.with([
  ['myBlockTypeHandle:entryDestination.entryThumbnail']
]).all() %}

{# Loop over the blocks (you can add the `if` statement back, if you like—this won't output anything if there are no blocks): #}
{% for item in menuItems %}
  {# Access the "first" related destination Entry—we already loaded it, so it should just be an array of everything attached to that field! #}
  {% set destination = item.entryDestination | first %}

  {# Same deal, we've already eager-loaded the nested `entryThumbnail` Asset(s), so we can just grab the first one: #}
  {% image = destination.entryThumbnail | first %}

  {# You could also {% dd image %} or {% dd destination %} if you want to examine either object! #}
  {{ image.getUrl() }}
{% endfor %}

The use of | first here is sort of confusing—all relational fields assume there can be multiple results, so they're returned as arrays when eager-loading. This filter just lets us safely access the first one (or returns null if the list is empty).

So, even though we may not be any not closer to definitively answering your question about the null values in the dd output, I hope this helps you narrow down your debugging!

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.