2

I have a couple paragraphs set up in my content type. I would like to overwrite the templates from my module (long story - we want to reuse this same set up across multiple sites if I get this to work).

I used the following hook in my module:

/**
 * Implements hook_theme_registry_alter().
 */
function my_module_theme_registry_alter(&$theme_registry) {
  $theme_registry['paragraph']['path'] = drupal_get_path('module', 'my_module') . '/templates/paragraph';
  $theme_registry['paragraph']['template'] = 'paragraph';
}

In my module's templates/paragraph folder I have the following files:

paragraph.html.twig
paragraph--accordion.html.twig
paragraph--carousel.html.twig

Here is theme debug:

<!-- THEME HOOK: 'paragraph' -->
<!-- FILE NAME SUGGESTIONS:
   * paragraph--accordion--default.html.twig
   * paragraph--accordion.html.twig
   x paragraph--default.html.twig
   * paragraph.html.twig
-->
<!-- BEGIN OUTPUT from 'modules/contrib/bootstrap_paragraphs/templates/paragraph/paragraph.html.twig' -->

It seems to work but the problem is that paragraph--accordion and paragraph--carousel always use paragraph.html.twig. How can I have paragraph--accordion and paragraph--carousel use the templates that are in my module. If I place them in my theme's template folder they do render.

1
  • In your module folder are you putting them in templates/paragraph? – Nick Dickinson-Wilde Oct 13 '16 at 18:17
1

If you want to place the twig files in the module's templates folder you have to use hook_theme():

function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'paragraph__accordion' => [
      'base hook' => 'paragraph',
    ],
    'paragraph__carousel' => [
      'base hook' => 'paragraph',
    ],
  ];
}
1
  • I think this way is better than using hook_theme_registry_alter(). – albertski Oct 13 '16 at 19:26
2

I was able to figure it out. I only used hook_theme_registry_alter(). You have the copy $theme_registry['paragraphs'] to all items you want to use paragraphs.

/**
 * Implements hook_theme_registry_alter().
 */
function my_module_theme_registry_alter(&$theme_registry) {
  $module_path = drupal_get_path('module', 'my_module');

  // Use the templates in my module's template folder.
  $theme_registry['paragraph__accordion'] = $theme_registry['paragraph'];
  $theme_registry['paragraph__accordion']['path'] = $module_path . '/templates/paragraph';
  $theme_registry['paragraph__accordion']['template'] = 'paragraph--accordion';
  $theme_registry['paragraph__carousel'] = $theme_registry['paragraph__accordion'];
  $theme_registry['paragraph__carousel']['template'] = 'paragraph--carousel';
}
3
  • I think it's important to add 'base hook' = paragraph, or else it won't inherit preprocess functions from paragraph's main theme, which could cause issues with compatibility with some contrib modules. – oknate Oct 13 '16 at 18:47
  • And better use hook_theme(), then you only need 'base hook' and drupal can discover the rest. The alter hook is more for manipulating existing theme hooks, not so much for generating new ones. You can do this, but there is more work to do. – 4k4 Oct 13 '16 at 19:10
  • @4k4 even though my way works I think you are right the later hook is meant for manipulating. I did try hook_theme and it worked as well. – albertski Oct 13 '16 at 19:25

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.