Notice: Undefined index: elements in block_theme_suggestions_block() 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-&gt;getConfiguration();
    $hello_messages = hello_messages_block_get_messages($config['amount_messages']);

    return array(
      '#theme' =&gt; 'block--hello_messages_block',
      '#type' =&gt; 'markup',
      '#content' =&gt; $hello_messages,
      '#attached' =&gt; array(
        'library' =&gt; 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' =&gt; array(
      'variables' =&gt; array('content' =&gt; NULL),
      'base hook' =&gt; '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 %}
    &lt;div class="message {{ key }}"&gt;
      &lt;div class="user_image"&gt;&lt;img src= "{{ value.image }}" /&gt;&lt;/div&gt;
      &lt;div class="username"&gt;{{ value.user }}&lt;/div&gt;
      &lt;div class="time"&gt;{{ value.time }}&lt;/div&gt;
      &lt;div class="message"&gt;{{ value.text }}&lt;/div&gt;
    &lt;/div&gt;
{% 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' =&gt; array(
      'variables' =&gt; array('content' =&gt; NULL),
      'template' =&gt; 'block--hello-messages-block',
    ),
  );
}
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:


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)
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.