Pranav Patel
Pranav Patel

Reputation: 121

Drupal 8 custom block (module) twig template file content not displaying

I have created a custom module and assigned a twig template file but it is not displaying. Below are files and folder structure

Folder structure

1.Code for workbuster.module file is as below

<?php

/**
 * Implements hook_theme().
 */
function workbuster_theme()
{
    return array(
    'block_workbuster' => array(
            'variables' => array('title' => NULL, 'description' => NULL),
            'template' => 'block--workbuster-custom',
        ),
    );
}

2. Code for WorkbusterBlock file is as below

<?php

/**
 * @file
 */
namespace Drupal\workbuster\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Workbuster' Block
 * @Block(
 * id = "block_workbuster",
 * admin_label = @Translation("Workbuster block"),
 * )
 */
class WorkbusterBlock extends BlockBase {

    /**
     * {@inheritdoc}
     */
    public function build() {

        return array(
            '#title' => 'Workbuster',
            '#description' => 'Workbuster'
        );
    }

}

3. Code for block--workbuster-custom.html.twig file is as below

{#
/**
 * @file
 * Profile Workbuster block.
 */
#}
 <div class="col-sm-3 ws-custom--block">
    <h1>{{ title }}</h1>
    <p>{{ description }}</p>
 </div>[![directory structure][1]][1]

Upvotes: 1

Views: 1259

Answers (1)

Santiago
Santiago

Reputation: 51

Try:

Inside your WorkbusterBlock.php you must set the template as follows:

 public function build() {
        return array(
          '#theme' => 'block__workbuster_custom',
          '#title' => 'Workbuster',
          '#description' => 'Workbuster'
        );
    }

In your .module, use your template as key:

function workbuster_theme()
{
    return array(
      'block__workbuster_custom' => array(
          'variables' => array('title' => NULL, 'description' => NULL),
        ),
    );
}

Note: I replaced hyphens (-) with underscores (_) in the template name

Upvotes: 5

Related Questions