1

I was looking for a way to show all fields/properties of a given object, and came across this useful tidbit: How to display all fields belonging to current entry?

I'm new to Craft/Twig and didn't actually know what type of object I'm trying to output properties of. I'm not sure when I output the object with {{ mysteryObject }} it outputs a number. That doesn't seem useful.

When I do the loop like in the question I referenced above like:

    {% for prop in mysteryObject.getFields() %}
        {# get the field Model from the fieldId #}
        {% set field = craft.fields.getFieldById(mysteryObject.fieldId) %}
        {# print the field handle and the field content  #}
        {{ field.handle }}
        :
        {{ entry.getContent()[field.handle] }}
    {% endfor %}

I get the error:

Calling unknown method: craft\elements\MatrixBlock::getFields()

Cool, so now I know my object is a MatrixBlock, but this makes me wonder how I would go about inspecting the properties of just any object, like you can in JavaScript, without knowing what the object is.

1 Answer 1

2

You can

<pre>
{{ dump(myObject) }}
</pre>

And see it's type and attributes. You can as well use {{ dump(Object.getAttributes) }} with nearly everything in Yii2. If it's just about getting to know what object you currently have this function will be your friend once you know how to use it. Another approach would be much simpler yet more powerful: take a look at the source code or enable intellisense for your IDE and let your IDE show you everything

2
  • Any idea why it outputs a number instead of doing dump by default?
    – BBaysinger
    Mar 19, 2019 at 22:50
  • 1
    In PHP there is toString method for objects so when you echo them, they'll be converted into a string. This is useful for entries (so they show their title) or for sections (to show their handle). So when you know what you are doing this might be a handy trick to shorten your syntax. The downside is: when you don't know what object you exactly have you might get in trouble because you don't even know you have an object. For example all redactor field values are objects as well and when you do {{ entry.text }} they'll output their stored html. Same goes for single selects. Mar 19, 2019 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.