What's wrong with my code to overwrite a block template? I want to render a template from a custom block. 

When I check the source with Twig debug enabled, I see the following output.

&lt;!-- THEME DEBUG --&gt;
&lt;!-- THEME HOOK: 'block' --&gt;
&lt;!-- FILE NAME SUGGESTIONS:
   * block--mozgraph.html.twig
   * block--moz-graph.html.twig
   * block--wk-elastic.html.twig
   x block.html.twig
--&gt;
&lt;!-- BEGIN OUTPUT from 'themes/custom/wundertheme/templates/block/block.html.twig' --&gt;
&lt;div id="block-mozgraph" class="contextual-region block-wk-elastic block-moz-graph"&gt;

      &lt;h2&gt;Moz: Graph&lt;/h2&gt;
    &lt;div data-contextual-id="block:block=mozgraph:langcode=en"&gt;&lt;/div&gt;


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-&gt;setHosts($hosts);
    $client = $client_builder-&gt;build();

    $query = array(
      'query' =&gt; array(
        'match_all' =&gt; array(),
      ),
    );

    $params = array(
      'index' =&gt; 'moz',
      'size' =&gt; 10000,
      'body' =&gt; $query,
    );


    $response = $client-&gt;search($params);

    return array(
      '#markup' =&gt; $this-&gt;t('Hello, World!'),
      '#theme' =&gt; 'block--wk-elastic',
    );
  }
}


What is wrong in my code?
You can create a directory called templates/ in your modules root. Place your template here.

Now let Drupal know you store the template in your module. in your_module.module add this function:

function YOUR_MODULE_theme($existing, $type, $theme, $path) {
  return array(
    'block__my_module' =&gt; array(
      'render element' =&gt; 'elements',
      'template' =&gt; 'block--my-module',
      'base hook' =&gt; 'block'
    )
  );
}


Finally be careful about where you place your twig file and what you name it. Create a templates directory in your module directory, and replace the _ in the theme function name with -: mymodule-block.html.twig

Don´t forget to clear the cache.



According to answer written by Nicensin in this post: Drupal 8 custom block (module) create twig template file