0

Hello I'm working on a module, I currently have an array with Node objects (custom content types), I am passing this array to a custom template (twig) but I can not render it with the values of its fields.

For example:

The dump object of the data that is coming to the template

array:2 [▼
  0 => Node {#564 ▼
    +in_preview: null
    #values: array:24 [▶]
    #fields: array:2 [▶]
    #fieldDefinitions: array:32 [▶]
    #languages: null
    #langcodeKey: "langcode"
    #defaultLangcodeKey: "default_langcode"
    #activeLangcode: "x-default"
    #defaultLangcode: "es"
    #translations: array:1 [▶]
    #translationInitialize: false
    #newRevision: false
    #isDefaultRevision: "1"
    #entityKeys: array:4 [▶]
    #translatableEntityKeys: array:7 [▶]
    #validated: false
    #validationRequired: false
    #loadedRevisionId: "9"
    #revisionTranslationAffectedKey: "revision_translation_affected"
    #enforceRevisionTranslationAffected: []
    #entityTypeId: "node"
    #enforceIsNew: null
    #typedData: EntityAdapter {#1065 ▶}
    #cacheContexts: []
    #cacheTags: []
    #cacheMaxAge: -1
    #_serviceIds: []
  }
  1 => Node {#562 ▼
    +in_preview: null
    #values: array:24 [▶]
    #fields: array:2 [▶]
    #fieldDefinitions: array:32 [▶]
    #languages: null
    #langcodeKey: "langcode"
    #defaultLangcodeKey: "default_langcode"
    #activeLangcode: "x-default"
    #defaultLangcode: "es"
    #translations: array:1 [▶]
    #translationInitialize: false
    #newRevision: false
    #isDefaultRevision: "1"
    #entityKeys: array:4 [▶]
    #translatableEntityKeys: array:7 [▶]
    #validated: false
    #validationRequired: false
    #loadedRevisionId: "10"
    #revisionTranslationAffectedKey: "revision_translation_affected"
    #enforceRevisionTranslationAffected: []
    #entityTypeId: "node"
    #enforceIsNew: null
    #typedData: EntityAdapter {#1198 ▶}
    #cacheContexts: []
    #cacheTags: []
    #cacheMaxAge: -1
    #_serviceIds: []
  }
]

As you can see, the data is inside the key #values

#values: array:24 [▼
      "vid" => array:1 [▶]
      "langcode" => array:1 [▶]
      "revision_uid" => array:1 [▶]
      "revision_timestamp" => array:1 [▶]
      "revision_log" => array:1 [▶]
      "revision_default" => array:1 [▶]
      "nid" => array:1 [▶]
      "type" => array:1 [▶]
      "uuid" => array:1 [▶]
      "isDefaultRevision" => array:1 [▶]
      "status" => array:1 [▶]
      "title" => array:1 [▶]
      "uid" => array:1 [▶]
      "created" => array:1 [▶]
      "changed" => array:1 [▶]
      "promote" => array:1 [▶]
      "sticky" => array:1 [▶]
      "default_langcode" => array:1 [▶]
      "revision_translation_affected" => array:1 [▶]
      "field_custom_title" => array:1 [▶]

    ]

The twig template:

<ul>
    {% for node in nodos %}
        <li>{{ node.field_custom_title}}</li> #not work
    {% endfor %}
</ul>

How do I get the values of the fields from twig?

0

The solution was simpler than I thought, it was simply using

{{ node.field_custom_title.value }}

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.