2

I have a Twig template for a custom block. My custom module is establishing what template is used like so

function mymodule_theme($existing, $type, $theme, $path){
    return array('mymodule' =>
        array(
            'variables' => array(
                'header-title' => 'Default Title',
                'header-size' => 'h1'
                ), 
                'template' => 'block--HeaderText'
            )
        );
}

and my custom block contains.

public function build() {
    return array(
        '#theme' => 'mymodule',
        '#title' => '',
        '#header-title' => $this->configuration['label'],
        '#header-size' => $this->configuration['header-size-submit'],
        );
}

And in my block--HeaderText.html.twig I have

<h2>{{ header-title }}</h2> This is a test {{ header-size }}
{{ dump() }}

However header-title and header-size both output 0. But using the dump() function in the template shows that the actual variables are available some how.

The dump() produces

array(15) { ["header-title"]=> string(19) "Header Textsdfgsdfg" ["header-size"]=> string(2) "h4"...

Yet {{ header-title }} just results in 0.

1 Answer 1

2

The issue was that Drupal didn't like that the variable names had hyphens in them. When switching from header-text to header_text it worked fine.

Your Answer

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

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