0

I've looked through many similar questions and can't figure out what I'm missing. I have a custom module with a block...

/**
 * Provides the 'Four Words' block
 *
 * @Block(
 *   id = "na_fourwords",
 *   admin_label = @Translation("Four Words Hero"),
 *   category = @Translation("Custom"),
 * )
 */
class NAFourWordsBlock extends BlockBase {

  public function build() {
    return [
      '#theme' => 'block__na_fourwords',
      '#myvar' => 'TESTING',
      '#attached' => ['library' => ['na_fourwords/na_fourwords']],
    ];
  }
}

In my .module file:

function na_fourwords_theme($existing, $type, $theme, $path){
  return [
    'block__na_fourwords' => [
      'variables' => [
        'myvar' => NULL,
      ],
    ],
  ];
}

In my twig, {{ myvar }} doesn't render. If I call {{ kint(elements.content) }}, I get ...

elements.content => Array
(
    [#theme] => block__na_fourwords
    [#myvar] => TESTING
    [#attached] => Array
        (
            [library] => Array
                (
                    [0] => na_fourwords/na_fourwords
                )

        )

)

It looks like the data is getting passed through ... why am I unable to access it? I feel like there's something simple I'm overlooking.

Drupal 9.1.7 PHP 7.4.16

Thanks!

1 Answer 1

0

So, block--na-fourwords.html.twig is an existing Drupal suggestion for a template name for a block with id na_fourwords. I was able to override the template file, but my theme function that was trying to talk to that file was being ignored because it already existed. It would be nice if Drupal had some kind of feedback to let you know of conflicting theme hooks like that.

Anyway, hopefully this saves someone similar frustration.

1

Your Answer

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

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