0

I did this in my custom module:

/**
 * Implements hook_theme().
 */
function my_module_theme() {
  return [
    'my_module' => [
      'template' => 'my-module'
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
 *
 * @param array $suggestions
 * @param array $variables
 */
function my_module_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  // Block suggestion for specific block bundle.
  if (isset($variables['elements']['content']['#block_content'])) {
    $block_bundle = $variables['elements']['content']['#block_content']->bundle();

    if ($block_bundle = 'my_block_bundle') {
      array_splice($suggestions, 1, 0, 'my_module');
    }
  }
}

I already created this block bundle in the module, so I want to use my template from module (my-module.html.twig in my_module/templates) for all blocks in this bundle, but with this code all my blocks in this bundle disappeared.

What I'm doing wrong here?

0

I haven't checked the rest of the code but

if ($block_bundle = 'my_block_bundle') {

should be

if ($block_bundle == 'my_block_bundle') {

// you could also use
$suggestions[] = 'your_suggestion' // instead of the splice
  • Thanks for the answer and I'll do as you suggested, but I don't have any problems with suggestion, I can see the suggestion in the twig debug, problem is to apply my template file from module... – mixerowsky Jul 27 '17 at 19:27
  • 1
    ah, it's working, like it's fixed by itself and I didn't changed anything...I'll accept your answer because of the suggestions...thanks again... – mixerowsky Jul 27 '17 at 19:46

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.