1

I am trying to add templates for the paragraph in a custom module, but it uses only the paragraph.html.twig template file.

paragraph--example-ch-hero-panel.html.twig is one of the suggested template files, which is the one I am using.

By default, The template should be taken from the paragraph, but it doesn't work in my case. I didn't implement hook_theme() in a module for this because I am not passing any value to the templates.

I tried the following code, but I got an error.

function example_ch_hero_panel_theme() {
  return [
    'paragraph__example_ch_hero_panel' => [
      'render element' => 'element',
      'template' => 'paragraph--example-ch-hero-panel',
      'base hook' => 'paragraph',
    ],
  ];
}

function example_ch_hero_panel_theme_registry_alter(&$theme_registry) {
  $theme_registry['page']['path'] = drupal_get_path('module', 'example_ch_hero_panel') . '/templates';
}

It throws the following exception.

Twig\Error\LoaderError: Template "modules/custom/example_ch_hero_panel/templates/page.html.twig" is not defined. in Twig\Loader\ChainLoader->getCacheKey()

Can anyone tell me how to fix this issue?

1
  • What theme suggestion are you trying to add exactly? For the paragraph type? For the paragraph in a certain panel? Is it about adding that from a custom module specifically? Or would it by OK to be added from your theme as well?
    – leymannx
    Jun 21, 2019 at 23:13

2 Answers 2

1

Your problem is coming from this code:

$theme_registry['page']['path'] = drupal_get_path('module', 'example_ch_hero_panel') . '/templates';

The first array key, page, means you are working the page template. You've told the system to look for page.html.twig in your module's /templates directory. It's not finding it, so you get that error.

Remove that hook altogether, it's not the right way to do what you are trying to do.

1
  • Yes, this is the explanation for the error the OP was seeing. This matches the code the OP was using. If then the OP got another error after removing the implementation of _theme_registry_alter(), that is a different matter. (It could simply be the OP needed to clear the cache, which could explain why the error comes from Twig\Loader\ChainLoader->getCacheKey().)
    – apaderno
    Jul 15 at 8:22
0

The following code is taken from a module that creates an accordion from a paragraph. (The module name has been changed.)

The paragraph--accordion.html.twig template file exists in mymodule/templates/paragraph.

function mymodule_theme($existing, $type, $theme, $path) {
  $themes['paragraph__accordion'] = array(
    'template' => 'paragraph--accordion',
    'base hook' => 'paragraph',
    'path' => \Drupal::service('extension.list.module')->getPath('mymodule') . '/templates/paragraph'
  );

  return $themes;
}

Your Answer

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