1

I'm new to Drupal, I create a module including block and twig. I want to call out my twig(modules/custom/footer_block/src/templates) and store data in there when execute build() in block.

However, it output a twig from theme folder(a templates folder inside the theme folder) that's is not created by me.

How can I force to output from my module folder?

The block is using the following code.

footerMenu_block.php.

public function build(){
    return [
        '#theme' => 'footer_menu',
        '#address_title' => $this->configuration['address_title'],
        '#address_content' => $this->configuration['address_content'],
    ];

footer-menu.twig.html

<div>
       <h2>{{ address_title }}</h2>
       <p>{{ address_content }}</p>
 </div>

footer_block.module

<?php
function footer_block_theme($existing, $type, $theme, $path){
  return array(
    'footer_menu' =>
      array(
        'variables' => array(
          'address_title' => '',
          'address_content' => '',
        ),
      ),
  );
}

If I move the Twig to that output folder (inside theme folder), it will be unable to store my data. If I make simple output '#markup' inside build() that will work fine. Therefore, other code in block should be working without problem.

I am using Drupal 8.6.x.

closed as off-topic by kiamlaluno Jun 5 at 8:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Sorry guys! I found out the problem, just because my templates folder put inside the src folder. they should be at the same level. – gdmorning520 Jun 5 at 4:24
0

Try adding the template array key to your theme hook:

function footer_block_theme($existing, $type, $theme, $path){
  return [
    'footer_menu' => [
      'variables' => [
        'address_title' => '',
        'address_content' => '',
      ],
      'template' => 'footer-menu',
    ],
  ];
}

Or try adding the block twig template suggestion as:

function footer_block_theme($existing, $type, $theme, $path){
  return [
    'footer_menu' => [
      'template' => 'footer-menu',
      'base hook' => 'block',
    ],
  ];
}
  • I tried both of them. Unfortunately, both of them are not working for me – gdmorning520 Jun 5 at 3:12
  • Your block is in the block list? Can be placed on page? Do you see theme suggestions for your block when inspecting element? – Prestosaurus Jun 5 at 3:26
  • Yes mate, my page can display the block title but not including content. – gdmorning520 Jun 5 at 3:41

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