How to override form element templates from custom module I have a custom field with some radio inputs and the following (widget-)output right now:

&lt;!-- BEGIN OUTPUT from 'core/themes/stable/templates/form/form-element.html.twig' --&gt;
&lt;div&gt;
  &lt;input /&gt;
  &lt;label&gt;&lt;/label&gt;
&lt;/div&gt;
&lt;!-- END OUTPUT from 'core/themes/stable/templates/form/form-element.html.twig' --&gt;


For every radio single radio input(In my case 5 times - back 2 back). Can I remove the outer &lt;div&gt; without actually touching any themes (neither the stable theme nor my custom theme) ?
Got it working. First you have to copy the default form element template into your module's /templates folder. For example:

$ mkdir -p modules/custom/MYMODULE/templates 
$ cp core/themes/stable/templates/form/form-element.html.twig modules/custom/MYMODULE/templates/form-element.html.twig


Now rename it to something you want. For example:

$ mv modules/custom/MYMODULE/templates/form-element.html.twig modules/custom/MYMODULE/templates/form-element--foo.html.twig


Edit the file to match your needs. For example remove or change the &lt;div&gt;&lt;/div&gt; tags. Finally in your module's MYMODULE.module file add the following two hooks.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 *
 * This hook adds a new theme suggestion for all radio form elements.
 */
function MYMODULE_theme_suggestions_form_element_alter(array &amp;$suggestions, array $variables) {

  if ($variables['element']['#type'] == 'radio') {
    $suggestions[] = 'form_element__foo';
  }
}

/**
 * Implements hook_theme().
 *
 * This hook registers a new theme in your module. It will
 * automatically look for the template in your module's /templates folder.
 */
function MYMODULE_theme($existing, $type, $theme, $path) {

  return [
    'form_element__foo' =&gt; [
      // base_hook is important!
      // Otherwise your template won't know what to render.
      'base hook' =&gt; 'form_element',
    ],
  ];
}


Only thing left, I find, is to have the form ID added to the template suggestion as well, so that we could target only this one form's elements. As now this template will be taken into account for all radio form element wrappers. Independent from what form they are in. But unfortunately I couldn't find the form ID inside the form_element's $variables.



Hah, you simply could add a class to your radios and identify them by that later.

$form['radios'] = [
  '#type' =&gt; 'radios',
  // ...
  '#attributes' =&gt; ['class' =&gt; ['foo']],
];


And then the hook_theme_suggestions_HOOK_alter could look like that:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYMOUDLE_theme_suggestions_form_element_alter(array &amp;$suggestions, array $variables) {

  if ($variables['element']['#type'] == 'radio' &amp;&amp; in_array('foo', $variables['element']['#attributes']['class'])) {
    $suggestions[] = 'form_element__foo';
  }
}


Now the custom template would only be taken into account for your specific radios.