Every time I want print a date I get this error.

Warning: Illegal offset type in isset or empty in Twig\Template->getAttribute()
Warning: array_key_exists(): The first argument should be either a string or an integer

I tried with the following, but I always get PHP warnings.

{{'now'|date('Y')  }}

{{ node.field_status_update_date.value|date('n/j/Y') }}

{{ node.field_status_update_date.value }}

  • Just use the renderable. {{ content.field_status_update_date }} – Kevin Feb 8 at 11:29

This field was empty, that is why I got this error. My only question: Do I have to check the if file is not empty every-time?

      {% if node.field_status_update_date.value %}

PS. The {{ content }} variables do not exist.

  • How does it not exist? Where are these being printed? There is not enough information here. – Kevin Feb 8 at 12:35
  • @Kevin I'm using page--node--nodetype.html.twig template. Only {{node}} is available there. – adpo Feb 8 at 13:13
  • Node is not a render array, its the entity object. Then yes, every time you try to use fields like that, you have to wrap them with a check first. This way is really not recommended, the recommended way would be doing it in the node template, or creating one or more view modes of the node, and creating Views blocks showing the current node in different view modes (so you can use different field formatting configurations) then putting the block in a given region. There is no entity render array in a page template, unless you preprocess and provide one. – Kevin Feb 8 at 14:06
  • @Kevin Thank you for your comment. This is the right answer. – adpo Feb 10 at 8:30

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.