I am trying to assign the variables to the return array from the custom controller (block). I have some data in $data array which I am passing with return array.

This is the code in CONTROLLER file:-

namespace Drupal\custom_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;

class HeaderBlock extends BlockBase {
    /**
     * {@inheritdoc}
     */

public function build() {

$data['test_var'] = '1234';
return array(
        '#type' => 'markup',
        '#theme' => 'block--header_block',
        '#desc' => 'Description here',
        '#data' => $data
      );
}

}

I'm not able to display the value of {{ desc }} or {{ data.test_var }} variables in custom block twig template.

I have also added below code in hook_theme() in custom module file.

MODULE_FILE:-

function mytheme_theme($existing, $type, $theme, $path) {

return array(
    'block__header_block' => array(
      'render element' => 'elements',
      'template' => 'block--header_block',
      'base hook' => 'block',
        'variables' => array(
          'data' => NULL
        )
    )
 );

}

Please let me know if anyone has this issue and resolved it.

Thanks

Comments

hiramanpatil created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ao2’s picture

I was in a similar situation.

One thing I noticed in the posted code is that in HeaderBlock::build(), at the line '#theme' => 'block--header_block',, the value contains hyphens instead of underscores.

However, even fixing that might not be enough.

I has some more luck by dropping the block__ and block-- prefixes everywhere (in the example above: in hook_theme(), in HeaderBlock::build(), and in the template file name.

After that the variables were picked up, but I was getting a message like this:

Notice: Undefined index: elements in block_theme_suggestions_block() (line 166 of core/modules/block/block.module).

That went away by removing these lines from hook_theme():

    'render element' => 'elements',
      'base hook' => 'block',

In particular the second one was triggering the message above.

BTW I think the 'template' line could be dropped too.

I am new to this part of Drupal, so I'd like confirmation that my indications make sense.

Thanks,
Antonio

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ivnish’s picture

Status: Active » Closed (works as designed)