1

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]

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.