I want to render a template from a custom block.
When I check the source with Twig debug enabled, I see the following output.
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'block' -->
<!-- FILE NAME SUGGESTIONS:
* block--mozgraph.html.twig
* block--moz-graph.html.twig
* block--wk-elastic.html.twig
x block.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/custom/wundertheme/templates/block/block.html.twig' -->
<div id="block-mozgraph" class="contextual-region block-wk-elastic block-moz-graph">
<h2>Moz: Graph</h2>
<div data-contextual-id="block:block=mozgraph:langcode=en"></div>
My module structure is the following.
wk_elastic
Plugin
Block
MozGraphBlock.php
templates
block--wk-elastic.html.twig
wk_elastic.info.yml
wk_elastic.services.yml
The code I use to build the block is the following.
/**
* @file
* Contains \Drupal\wk_elastic\Plugin\Block\MozGraphBlock.
*/
/**
* Provides a 'MOZ: Graph' block.
*
* @Block(
* id = "moz_graph",
* admin_label = @Translation("Moz: Graph")
* )
*/
namespace Drupal\wk_elastic\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Elasticsearch;
class MozGraphBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$hosts = array(
'192.168.10.10',
);
$client_builder = Elasticsearch\ClientBuilder::create();
$client_builder->setHosts($hosts);
$client = $client_builder->build();
$query = array(
'query' => array(
'match_all' => array(),
),
);
$params = array(
'index' => 'moz',
'size' => 10000,
'body' => $query,
);
$response = $client->search($params);
return array(
'#markup' => $this->t('Hello, World!'),
'#theme' => 'block--wk-elastic',
);
}
}
What is wrong in my code?