10

I want to create my custom block with custom content in Drupal 8,in start I create Block with just simple text on it, and work correctly, my Problem is how can I pass my custom variable to it? I saw some tutorial about it , my Module name is tcdev and this is content of .module file.

function tcdev_theme($existing, $type, $theme, $path) {
    return array('tcdev' =>
        array(
            'variables' => array(
                'title' => 'Default title',
                'description' => null
            ),
            'template' => 'block--sliderblock'
        )
    );
}

and my SliderBlock.php

    namespace  Drupal\tcdev\Plugin\Block;
use Drupal\Core\block\BlockBase;


/**
 * Provides a 'SliderBlock' block.
 *
 * @Block(
 * id = "slider_block",
 * admin_label = @Translation("Slider Block"),
 * )
 */

class SliderBlock extends BlockBase{
    /**
     * {@inheritdoc}
     */
    public function build(){
        return array(
            '#title' => 'my title ',
            '#description' => 'my custom desc'
        );
}
}

and finally my template file block--sliderblock.html.twig

  <h2>My Custom Block{{ title }}-{{ description }}</h2>

But Result is only "My Custom Block-" . The problem is variables not getting passed to template file,where is my fault?

appreciate any help

12

Your return array doesn't contain a #theme key. So at the moment you aren't using a template file at all.

And you probably have a mix up between the outer block and theming the content inside of the block. Your template inside has the name tcdev, because that is the name you defined at the top of the array in *_theme(), and tcdev.html.twig is where your variables will end up.

Your code would look like the following one.

function tcdev_theme($existing, $type, $theme, $path) {
  return array('tcdev' =>                  // this is the name of the template
           array(
            'variables' => array(
                'title' => 'Default title',
                'description' => null
            ),
        )
    );
}

SliderBlock.php

namespace  Drupal\tcdev\Plugin\Block;
use Drupal\Core\block\BlockBase;


/**
 * Provides a 'SliderBlock' block.
 *
 * @Block(
 * id = "slider_block",
 * admin_label = @Translation("Slider Block"),
 * )
 */

class SliderBlock extends BlockBase{
    /**
     * {@inheritdoc}
     */
    public function build() {
        return array(
            '#theme' => 'tcdev',
            '#title' => 'my title ',
            '#description' => 'my custom desc'
        );
   }
}

tcdev.html.twig

  <h2>My Custom Block{{ title }}-{{ description }}</h2>

You wrote that you saw some tutorials about it. Probably this was about two different topics, and you are trying to apply them at the same time.

The custom template in the first link is tcdev.html.twig. What you are trying to do with the method in the second link is for block--sliderblock.html.twig.

  • But block--sliderblock.html.twig rendered, the problem is how pass my variable to it !!!? what is you suggest to me according to my code. ? tnx for your attention – Yusef Jun 2 '16 at 10:57
  • You want the content of the variables inside a block? Then this is not connected to a block template, because this is for the outside of the block. (block--sliderblock.html.twig rendered, because you used the name of a theme suggestion, this would render even if the block would be totally empty, you can try this) – 4k4 Jun 2 '16 at 11:37
  • I tried to change your code to make things clearer, hope this works, didn't debug it. – 4k4 Jun 2 '16 at 11:51
  • I apply your code, rebuild the cache, but still read from block--sliderblock.html.twig and nothing passed to it . I enable debug info and debug info is <!-- END OUTPUT from 'themes/bootstrap/templates/block/block--system-branding-block.html.twig' <!-- FILE NAME SUGGESTIONS: x block--sliderblock.html.twig * block--slider-block.html.twig * block--tcdev.html.twig * block.html.twig --> <!-- BEGIN OUTPUT from 'themes/gttc_2016/templates/block--sliderblock.html.twig' --> <h2>Block-</h2> – Yusef Jun 2 '16 at 13:20
  • Your variables are not read by block--sliderblock.html.twig. This templates works with its own set of variables. (If you want to use this twig you would have to copy it from core or base theme and then you see what variables are inside). Your variables set in the render array of the build function will end up in tcdev.html.twig. Did you create this twig file? – 4k4 Jun 2 '16 at 13:28
3

So that I found, you want to overwrite a default block template with a custom block (module) template, and then pass your variables to it, first of all you must make sure overwrite your template correctly because seems your current block template outputed from your Theme directory (not module):

<!-- BEGIN OUTPUT from 'themes/gttc_2016/templates/block--sliderblock.html.twig' -->

To do this first you should create a directory called templates/ in your modules root and then place your template there.

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' => array(
      'render element' => 'elements',
      'template' => 'block--my-module',
      'base hook' => '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.


With the help of the answer written by Nicensin in this post:
Drupal 8 custom block (module) create twig template file

  • 1
    What about the variables OP asked for? – leymannx Nov 30 '16 at 12:25
1

I got to this point too. The variables don't seem to be pass to to the block template. I have almost the same code as you, in my case I enabled twig debug and disable cache in services.yml. Even clearing cache I was having this issue but got solved once I activated this config in my dev environment.

twig.config:    
# Not recommended in production environments
# @default false
debug: true
# Not recommended in production environments
# @default null
auto_reload: true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.