3

i need to override a form.html.twig in my custom module.

i used hook_theme_suggestions_alter in my .theme file to add new suggestions.

function MY_THEME_theme_suggestions_alter(array &$suggestions, array $variables){

if (isset($variables['element']) && isset($variables['element']['#type']) && $variables['element']['#type'] == 'form') {
    $original_theme_hook = $variables['theme_hook_original'];
    $suggestions[] = $original_theme_hook . '__' . str_replace('-', '_', $variables['element']['#id']);
}

return $suggestions;
}

the debug show me this suggestions

THEME HOOK: 'form' -->
<!-- FILE NAME SUGGESTIONS: * form--pom-mapping-setting-form.html.twig x form.html.twig -->
<!-- BEGIN OUTPUT from 'core/modules/system/templates/form.html.twig' -->

if i put form--pom-mapping-setting-form.html.twig in my_theme/templates/ folder works.

if i put form--pom-mapping-setting-form.html.twig in my_module/templates folder doesn't works

I don't understand why :/

why drupal doesn't read templates in module folder?

thanks

0

You need to alter the Theme registry and set the path of the hook suggestion "form" to your_module/template path as bellow:

/**
 * Implements hook_theme_registry_alter().
 */
function My_Module_theme_registry_alter(&$theme_registry) {
  // You can add some conditions here in case you want
  // to override the form template only for some reasons.
  $theme_registry['form']['path'] = drupal_get_path('module', 'My_Module') . '/templates';
}

And the structure of your module should be:

-- My_Module
  +-- templates
     +-- form--pom-mapping-setting-form.html.twig
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy