- Issue created by @aubjr_drupal
- 🇳🇱Netherlands tinto Amsterdam
I've been looking for something like this many times. Even a primer with some general examples would be very helpful to so many users.
If I'm a TWIG noob and I'm looking for a value in a TWIG file to render it, there's a lot of documentation on how to get to $_context and how to set up debugging, but there's no real help anywhere for interpreting/understanding $_context. And trial and error is extremely painful with such a large variable to traverse.
I've put together 1) a general set of rules for noobs that explains how to more simply look for field text/values in $_context:
This assumes that xdebug is working for you, or that you have {{ dump() }} or {{ kint() }} working in the TWIG file in question.
$_context
.$_context->node
. At the node-level, it's just $_context
.$_context
).$context["node"]->fields["uid"]["x-default"]->list[0]->values["target_id"]
and find the uid, but there's no user object with the name property. You'll instead find the user name deeper, under: $context["node"]->fields["uid"]["x-default"]->list[0]->properties["entity"]->target->entity->fields["name"]["x-default"]->list[0]->values["value"]
- which will be {{ node.uid.entity.name.value }}
in TWIG)Now that you've found the value in $_context, build your Twig chain by cleaning house:
<ul>
<li>["x-default"]</li>
<li>target-></li>
<li>list[N]-></li>
<li>entity-></li>
<li>(Any other property that has no array key attached to it)</li>
</ul>
$context["node"]->fields["field_header_image"]->properties["entity"]->fields["field_media_image"]->properties["entity"]->values["uri"]
node.field_header_image.entity.field_media_image.entity.uri
"values"['<key>']
associative array with a "value" => "" structure, add ".value" to the end:node.field_header_image.entity.field_media_image.entity.uri.value
While this documentation won't work for every scenario, it shoud give noobs that don't get $_context at all a few guardrails to speed up their comprehension. Is there anywhere that this documentation would make sense on drupal.org?
Active
Missing documentation
I've been looking for something like this many times. Even a primer with some general examples would be very helpful to so many users.