Add variables in custom input template In D8 I created a custom input twig file (based on input.html.twig) : 

{{ icon }}

&lt;input{{ attributes }} /&gt;{{ children }}


Then I created a new entry in hook_theme() :

function my_module_theme() {
 return [
   'input_custom' =&gt; [
     'render element' =&gt; 'form',
     'variables' =&gt; [
       'icon' =&gt; NULL,
     ],
     'template' =&gt; 'input-custom',
   ],
 ];
}


Then I'm loaded the template in my form element :

$form['my_field'] = [
        '#type' =&gt; 'textfield',
        '#theme' =&gt; 'input_custom',
        '#icon' =&gt; 'my_icon',
        '#title' =&gt; $this-&gt;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 ?
Declare a #theme starting with the base hook of the input element and a double underscore:

  $form['my_field'] = [
    '#type' =&gt; 'textfield',
    '#theme' =&gt; 'input__custom',
    '#icon' =&gt; 'my_icon',
    '#title' =&gt; $this-&gt;t('My title'),
  ];


Prepare additional variables in a preprocess hook:

function mytheme/mymodule_preprocess_input(&amp;$variables) {
  if ($variables['theme_hook_original'] == 'input__custom') {
    $variables['icon'] = $variables['element']['#icon'];
  }
}


Then you can use the added variables in the input template:

{{ icon }}