1

I have created a custom module. in that module I have create a custom twig template using hook_theme function

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_settings' => array(
      'variables' => array(
        'role' => NULL,
      )
    )
  );
}

Then I created a custom block using my module here is the code of the block

public function build() {
    $config = \Drupal::config('mymodule.config');
    $role = $config->get('role');
    return array(
      '#theme' => 'mymodule_settings',
      '#role' => $role,
    );
  }

I created a templates directory in my module and inside that directory I have created mymodule-settings.html.twig file.

Cleared the cache but Drupal gives me the following error.

Twig_Error_Loader: Template "themes/subtheme/templates/mymodule-settings.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/subtheme/templates/mymodule-settings.html.twig" in the Drupal theme registry.). in Twig_Loader_Chain->getCacheKey() (line 45 of themes/basetheme/templates/block.html.twig).

My theme is a subtheme. Why Drupal is looking for the template in the theme and not in my module?

  • This should be working, are you sure it is nog a typo in the hook_theme name? Cleared cache? – JurgenR Oct 12 '17 at 12:51
  • The problem was, I was using the same name for custom module and my subtheme. Should I delete the question? – Ahmad Oct 17 '17 at 8:09
-1

Try like this:

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_settings' => array(
      'variables' => array(
        'role' => NULL,
      )
      'template' => 'mymodule-settings',
    )
  );
}

You are missing to define template.

  • 1
    Template doesn't have to be defined, when omitted it searches for mymodule-settings.html.twig by default. – JurgenR Oct 12 '17 at 12:49
  • I think it searches by default in theme template folder, not module folder. I am not specific on this but for my case it happened and I defined template in hook_theme(). – Ajay Reddy Oct 12 '17 at 13:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.