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 ?
'render element' => 'form'
from your hook_theme, I don't think you want that if you're trying to specify the variables as well.