Add variables in custom input template

05/07/2018


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 customer input template ?

قالب وردپرس