2

I have a render array for a form in my D7 module that looks like this:

'form' => 
  array
    '#id' => string 'demo-form' (length=9)
    '#action' => string '/sprint07/' (length=10)
    'name' => 
      array
        '#title' => string 'Username' (length=8)
        '#maxlength' => int 13
        '#size' => int 15
        '#type' => string 'textfield' (length=9)
        '#required' => boolean true
        '#input' => boolean true
        '#autocomplete_path' => boolean false
   ... snip ...

I'm using twig-for-drupal to create a theme, but I can't seem to get the values back, I've tried {{ form['name']['#type'] }}, {{ form.name.#type }}, {{ form.name['#type'] }} and so on, but they all come back blank.

Other fields such as form['#action'] works fine, so how should I access the information more than one array in? I can't change the module too much as the php template in its native state uses render(form);

-- Update --

Added some more debugging to this, and seems that I can use {{ form|dump('v') }} to show me the whole array as above, but when I do {{ form.name|dump('v') }} I just get a string, which I assume means that twig is automatically detecting its a render array, and "helpfully" converting it for me.. So I guess that makes my question: how do I turn off the auto-rendering?

2 Answers 2

1

"name" sub array don't contain the "#id" key.

try if work

{{ form.name.#title }} 
Sign up to request clarification or add additional context in comments.

1 Comment

id was a bad example, I have the same problem for the keys that do exist as well.
0

Turns out it isn't currently possible to do this on a case-by-case basis, TFD is currently set up to auto-render anything it thinks is a render array, and to turn this off you need to set autorender to FALSE in twig_get_instance(). I've tried this myself however and it still seems to be turning the array into a string, so this could be a bug within TFD, or I just don't understand the usage, which is equally, if not more, likely.

From ReneB's sandbox:

Autorender

This version of the TWIG engine uses auto render to prevent themers get RSI from typing {{node.field_somefield|render}} for every single field they want to render from the render array (of doom) so the can safely type {{node.field_something}}

On rendering of the compiled template TFD check if the called variable is a string, callable or array. If it's a string it simple does echo $string, if it's a callable it return a proper method() for it. And if it's an array, it assumes it's a renderable array and maps it to the render($string); method of drupal.

This way the objects hidden with hide() are respected.

To turn this is for now, you have to set autorender to FALSE in the twig_get_instance() method of twig.engine.

I'am working on a {% noautorender %} {% end noautorender %} block structure.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.