1

I created block in the custom module. This block works, but I see Notice in the Recent log messages.

Notice: Undefined index: elements in block_theme_suggestions_block() (line 179 of /var/www/html/drupal-8.3.x-dev/core/modules/block/block.module)

I see block_theme_suggestions_block().

function block_theme_suggestions_block(array $variables) {
  $suggestions = array();
  $suggestions[] = 'block__' . $variables['elements']['#configuration']['provider'];

And for my block $variables['elements']['#id'] is NULL.

I can't understand why my block can't pass the ID as other blocks.

The code I am using is the following.

/**
 * @Block(
 *   id = "hello_messages",
 *   admin_label = @Translation("Hello messages"),
 * )
 */
class HelloMessagesBlock extends BlockBase {

  // ....

  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = $this->getConfiguration();
    $hello_messages = hello_messages_block_get_messages($config['amount_messages']);

    return array(
      '#theme' => 'block--hello_messages_block',
      '#type' => 'markup',
      '#content' => $hello_messages,
      '#attached' => array(
        'library' => array(
          // Custom .css styles for block.
          'hello_messages_block/hello_messages_block',
        ),
      ),
    );
  }
}
/**
 * Implements hook_theme().
 */
function hello_messages_block_theme($existing, $type, $theme, $path) {
  return array(
    'block--hello_messages_block' => array(
      'variables' => array('content' => NULL),
      'base hook' => 'block',
    ),
  );
}

This is the content of block--hello-messages-block.html.twig.

{% extends "block.html.twig" %}
{% set attributes = attributes.addClass('block-hello-messages') %}
{% block content %}
{% for key, value in content %}
    <div class="message {{ key }}">
      <div class="user_image"><img src= "{{ value.image }}" /></div>
      <div class="username">{{ value.user }}</div>
      <div class="time">{{ value.time }}</div>
      <div class="message">{{ value.text }}</div>
    </div>
{% endfor %}
{% endblock %}

Why doesn't my block pass $variables['elements']['#id'] to the theme suggestion hook?

UPDATE:

Thank you, Berdir. This solution helps me.

function hello_messages_block_theme($existing, $type, $theme, $path) {
  return array(
    'block--hello-messages-block' => array(
      'variables' => array('content' => NULL),
      'template' => 'block--hello-messages-block',
    ),
  );
}

2 Answers 2

3

You are not supposed to use the block template like this. This will be added automatically and it will be done so one level higher in the render array structure.

Check other block plugins, none of them use #theme like this, like SystemBrandingBlock::build()

You have two options:

  1. Define the block template suggestions like system_theme() does it and then overidde the output there. (the first in the list is the one for the branding block linked above, so you can easily connect the two things)

  2. Make your own template for the content within the block. It will be wrapped by a generic block template, so you don't have to worry about that at all.

1
  • @Berdir, great answer, but i think method 1) should not be used in standard use case. In core it is only used for the system branding block. It has the side effect that the normal content of the block is ignored. This may cause some trouble if someone wants to change the block content in theming. Content in block is not different from controllers and this is what method 2) describes. It is not only for own templates but also for render elements and templates that already exists.
    – 4uk4
    Commented Sep 4, 2016 at 12:09
0

I've found that this notice is triggered when I include an extra underscore in my theme suggestion. This would not apply to your particular case, so I won't adapt my answer to your code. This is another way that this particular notice can be triggered, so hopefully it's helpful for someone who finds themselves here (probably future me). I can't explain why, I can only say that it's true.

Doesn't trigger the notice:

In block plugin build() method:

$menu_build['#theme'] = "menu__group_menu_breadcrumbs_admin";
return $menu_build;

In .module file:

/**
 * Implements hook_theme().
 */
function mymodule_groups_theme($existing, $type, $theme, $path) {
  $path .= '/templates';
  return [
    'menu__group_menu_breadcrumbs_admin' => [
      'template' => 'menu--group-menu-breadcrumbs--admin',
      'path' => $path . '/menu',
      'base hook' => 'block',
    ],
  ];
}

Triggers the notice:

In block plugin build() method:

$menu_build['#theme'] = "menu__group_menu_breadcrumbs__admin";
return $menu_build;

In .module file:

/**
 * Implements hook_theme().
 */
function mymodule_groups_theme($existing, $type, $theme, $path) {
  $path .= '/templates';
  return [
    'menu__group_menu_breadcrumbs__admin' => [
      'template' => 'menu--group-menu-breadcrumbs--admin',
      'path' => $path . '/menu',
      'base hook' => 'block',
    ],
  ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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