0

My custom module theme template is not render by Drupal for some reasons.

In my .module file I implement the hook_theme and define my theme settings:

/**
 * Implements hook_theme().
 */
function promotional_overlay_block_theme($existing, $type, $theme, $path) {
  return [
    'promotional_overlay_block' => [
      'variables' => [
        'config' => NULL,
      ],
    ],
  ];
}

I then implement the #theme in hook_preprocess_block variable:

 /**
  * Implements hook_preprocess_block().
  */
 function promotional_overlay_block_preprocess_block(&$variables) {
  $blockContent = $variables['content']['#block_content'];
  $variables['promotionalOverlay'] = [
        '#theme' => 'promotional_overlay_block',
        '#config' => ['title' => 'hello world', 'body' => 'test body content'],
        '#cache' => [
          'keys' => ['promotional_overlay_block', $blockContent->id()],
          'tags' => $blockContent->getCacheTags(),
          'max-age' => 0,
        ]
      ];
    }

I do have my template file in the following location:

templates/promotional-overlay-block.html.twig

So, I assign my custom block to the footer region of the page, and it doesn't render through my template. It just show up the default block render without the template in my module. I did search the page HTML source code, and I can't see my twig template there.

My Question, how can I theme my block using my template file?

  • 1
    If you use the default block template try to change $variables['promotionalOverlay'] to $variables['content']['promotionalOverlay'] – 4k4 May 28 '19 at 7:31
  • 1
    By the time you're in the preprocess, the theme hook has already been determined as block, and you're preprocessing variables for that theme hook. I haven't done this in D8, but theoretically you should be able to set the #theme for the block in hook_block_alter() (api.drupal.org/api/drupal/…). – Jaypan May 28 '19 at 8:00
  • As I understand the goal is not to replace block but to add content to the block, which is defined by a custom template. The same way as you could add #markup or any other render element. – 4k4 May 28 '19 at 8:11
  • @4k4 - That's correct, I want to theme the block content. – fkaufusi May 28 '19 at 8:14

Your Answer

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

Browse other questions tagged or ask your own question.