Drupal 8 user block (module) creates a twig template file

I have a custom module that creates a custom block with field elements.

This all works great, but I need to split this block. I checked other posts here and tried with no luck.

I turned on twig debugging and got suggestions on the topic. Not lucky yet.

Can someone point me in the right direction.

This is what I have so far:

my_module / my_module.module

// nothing related in here

my_module / src / plugin / block / myModuleBlock.php

 <?php namespace Drupal\my_module\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; /** * Provides a 'ModuleBlock' block. * * @Block( * id = "module_block", * admin_label = @Translation("My Module"), * ) */ class ModuleBlock extends BlockBase { public function blockForm($form, FormStateInterface $form_state) { $form['test'] = array( '#type' => 'select', '#title' => $this->t('test'), '#description' => $this->t('test list'), '#options' => array( 'Test' => $this->t('Test'), ), '#default_value' => isset($this->configuration['test']) ? $this->configuration['test'] : 'Test', '#size' => 0, '#weight' => '10', '#required' => TRUE, ); return $form; } /** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['test'] = $form_state->getValue('test'); } /** * {@inheritdoc} */ public function build() { $build = []; $build['module_block_test']['#markup'] = '<p>' . $this->configuration['test'] . '</p>'; return $build; } } 

my_module / templates / block - my-module.html.twig // as suggested by twig debugging

 <h1>This is a test</h1> <div id="test-widget">{{ content }}</div> 

I should also note that in my my_theme.theme I have this, but I don't think it matters:

 // Add content type suggestions. function my_theme_theme_suggestions_page_alter(array &$suggestions, array $variables) { if ($node = \Drupal::request()->attributes->get('node')) { array_splice($suggestions, 1, 0, 'page__node__' . $node->getType()); } } 

What I tried:

 public function build() { return array( '#theme' => 'block--my-module' ); } 

But still no.

Any help here is much appreciated.

UPDATE: I just got it to work, but I still need help. I moved the block--my-module.html.twig template block--my-module.html.twig to my themes directory and it worked.

How do I make it work in my modules directory?

+6
source share
2 answers

UPDATE: I just got it to work, but I still need help. I translated the template block - my-module.html.twig into my theme directory and it worked.

How do I make it work in my modules directory?

You can create a directory called templates/ in the root of the modules. Put your template here.

Now let Drupal know that you are storing 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' => array( 'render element' => 'elements', 'template' => 'block--my-module', 'base hook' => 'block' ) ); } 

This is not verified. This is the way it worked for my user block.

Remember to clear the cache.

+6
source

To add a twig file to your module, you need to make sure that the module defines the link, not the topic.

You can still implement hook_theme () in the module.module file as follows:

 function mymodule_theme($existing, $type, $theme, $path) { return [ 'mymodule_block' => [ 'variables' => [ // define defaults for any variables you want in the twig file 'attributes' => [ 'class' => ['my-module-class'], ], //etc ], ], ]; } 

Then in the implementation of block build () you can add a link to a new theme function:

 public function build() { // Load the configuration from the form $config = $this->getConfiguration(); $test_value = isset($config['test']) ? $config['test'] : ''; $build = []; $build['#theme'] = 'mymodule_block'; // You would not do both of these things... $build['#test_value'] = $test_value; $build['module_block_test']['#markup'] = '<p>' . $test_value . '</p>'; return $build; } 

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

+5
source

All Articles