1

I created a custom form, a custom template, and I "linked" both with a theme hook.

In the buildForm() method, I used the following code.

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#foo'] = array("key_a"=>"value_a", "key_b"=>"value_b", "key_c"=>"value_c");
  // …
  return $form;
}

From the template, when I show all the available variables with {{ dump() }} or {{ kint() }} I see #foo is present, but I cannot print its value.

I tried the following code, but either I get an error or NULL is printed.

{{ element.#foo }}
{{ element.'#foo' }}
{{ 'element.#foo' }}
{{ foo }}

How can I print that value?

3

Like javascript you need to use bracket notation to get hash key variables:

{{ element['#foo'].key_a }}

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.