0

I create a module and I create a template for that .

I use this code in mymodule.module :

function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'moduletheme' => [
      'render element' => 'form',
      'variables' => array('test_var' => NULL),
    ],
  ];
}

And I use this code in mymodule/src/Form/MymoduleForm.php :

public function buildForm(array $form, FormStateInterface $form_state) {
    $forms['#theme'] = [
        '#theme' => 'moduletheme',
        '#test_var' => $this->t('Test Value'),
    ];
    return $forms;
}

And I use this code in mymodule/templates/moduletheme.html.twig :

<p>Test twig template!</p>
<p>test_var: {{ test_var }}</p>

But when I clear cache and refresh my module page , I just see :

Test twig template!
test_var:

How can I fix this problem in drupal 8 ?

2
  • Try removing 'render element' => 'form' from your hook_theme, I don't think you want that if you're trying to specify the variables as well.
    – benjy
    Feb 14 '17 at 11:08
  • I removed that but still not work
    – RobinSon
    Feb 14 '17 at 12:35
1

Remove the '#' from the form field key and clear cache. $forms['theme'] instead of $forms['#theme'].

$forms['theme'] = [
    '#theme' => 'moduletheme',
    '#test_var' => $this->t('Test 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.