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',
),
);
}