In D8 I created a custom input twig file (based on input.html.twig) :
{{ icon }}
<input{{ attributes }} />{{ children }}
Then I created a new entry in hook_theme() :
function my_module_theme() {
return [
'input_custom' => [
'render element' => 'form',
'variables' => [
'icon' => NULL,
],
'template' => 'input-custom',
],
];
}
Then I'm loaded the template in my form element :
$form['my_field'] = [
'#type' => 'textfield',
'#theme' => 'input_custom',
'#icon' => 'my_icon',
'#title' => $this->t('My title'),
];
My custom variable is sent to the template but I lost all html attributes on the input. If I remove variables
from my_module_theme()
all html attributes are back, but I'm losing my custom variable.
How can I send custom variables in custom input template ?
render element
is wrong what am i suppose to use ? It's not a duplicate question, here it's about a specific element. Your link is about the template of the form (i'm already using this and it's working :))'render element' => 'input'
.'#theme' => 'input__custom'
. No need to redeclare this template in hook_theme() then. You can prepare additional variables in a preprocess hook.