1

I am using the following code, but it doesn't work.

drupal_block.module

function custom_blocks_theme($existing, $type, $theme, $path) {
  return [
    'custom_blocks__front_apps' => [
      'variables' => [
        'app' => NULL
      ],
    ],
  ];
}

BlockTest.php

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#theme' => 'custom_blocks__front_apps',
      '#app' => 'test value',
    ];
  }

custom_blocks__front_apps.html.twig

<p>Hello: {{ app }}</p>

My block is empty.

enter image description here

What is wrong with the code?

0

2 Answers 2

3

I think you should write it like below code.

   /**
    * Implements hook_theme() to add the template definition. 
    **/
    function lotus_theme($existing, $type, $theme, $path) {
      return [
        'lotus_template' => [
          'variables' => ['test_var' => NULL],
        ],
      ];
    }

In your controller file

public function build() {

    return array[
      '#theme' => 'lotus_template',
      '#test_var' => $this->t('Test Value'),
    ];

  }

Then in your lotus-template.html.twig

<p> This is the lotus template with a value of {{ test_var }} </p>

Source Define a custom template for module output

3
  • It still empty! i really changed by your way... but nothing.... still empty . but one thing... it shouldnt be lotus_template.html.twig ? -- I cleared cache -- I uninstalled and then installed again.. Commented Apr 25, 2018 at 15:22
  • @OscarR.GamboaA. If you are editing the code of an installed module, uninstall it, make the changes in the code, and then install it again.
    – avpaderno
    Commented Apr 25, 2018 at 16:07
  • @kiamlaluno do you have any repository? i still have no success about it :( Commented Apr 25, 2018 at 20:45
1

Finallly i solved my own problem... but i don't know what exactly it is... Drupal follow strictly their names theme and if i get wrong in something small the block crashes.. but i'll show you how it is working now the code.

blocktest.module

/**
 * Implements hook_theme() to add the template definition.
 **/
function blocktest_theme($existing, $type, $theme, $path) {
    return array(
        'block-test' => array(
            'variables' => array('test_var' => NULL),
        ),
    );
}

Plugin/Block/BlocktestController.php

namespace Drupal\blocktest\Plugin\Block;
use  Drupal\Core\Block\BlockBase;


/**
 * @Block(
 *   id = "block_test",
 *   admin_label = @Translation("Block Test"),
 * )
 */
class BlocktestController extends BlockBase
{

    /**
     * Builds and returns the renderable array for this block plugin.
     *
     * If a block should not be rendered because it has no content, then this
     * method must also ensure to return no content: it must then only return an
     * empty array, or an empty array with #cache set (with cacheability metadata
     * indicating the circumstances for it being empty).
     *
     * @return array
     *   A renderable array representing the content of the block.
     *
     * @see \Drupal\block\BlockViewBuilder
     */
    public function build()
    {
        return array(
            '#theme' => 'block-test',
            '#test_var' => $this->t('Test Value'),
        );
    }
}

block-test.html.twig

<p> This is the lotus template with a value of {{ test_var }} </p>
<button>it works</button>

Thanks!

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