1

First question - but I think this is a bug (not my fault).

I have entries/pages (a structure) - one parent (product overview) with children (products)

I have one category group with categories - only for assigning some repeating data to the child entries (like properties of products - colors for instance)

Getting the properties (the field data of the assigned category) on the product pages itself works flawless like this:

{% set properties = entry.fieldPropertiesCategory.one() %}
{{ properties.propCatFieldColor }}

BUT on my product's list page (the above-mentioned parent) it doesn't. I try the following:

{% set products = craft.entries.descendantOf(entry).all() %}
{% for product in products %}
    {% set properties = product.fieldPropertiesCategory.one() %}
    {{ properties.propCatFieldColor }}
{% endfor %}

I get "Impossible to access an attribute ("propCatFieldColor") on a null variable."

But properties isn't null (checked this with dump()) ... it's the same like on the children entries.

My workaround is the use of all() and an additional iteration - this surprisingly works:

 {% set products = craft.entries.descendantOf(entry).all() %}
 {% for product in products %}
    {% set properties = product.fieldPropertiesCategory.all() %}
    {% for property in properties %}
       {% if loop.first %}
          {{ properties.propCatFieldColor }}
       {% endif %}
    {% endfor %}
{% endfor %}

Makes no sense to me ...

I tried everything - getting the category object of the children entries in various ways on the parent entry but this all doesn't work. Always the impossible to access error.

Has anyone an idea on this?

1

It feels like not all your products have a fieldPropertiesCategory value defined. Does that work?

{% set products = craft.entries.descendantOf(entry).all() %}
{% for product in products %}
    {% set properties = product.fieldPropertiesCategory.one() %}
    {{ properties ? properties.propCatFieldColor : '' }}
{% endfor %}
| improve this answer | |
  • I really like your feelings, Brad! :) I didn't had the iteration in mind. So clear now: I "configured" only my first product yet. The error was related to the products with no category selected. THANK YOU SO MUCH!!! – Eran Dax Lonker Mar 31 at 6:25

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.