0

I'm attempting to use arrays in a twig template from variables in a custom block.

In my custom block I have

public function build() {
    $query = \Drupal::entityQuery('node')
        ->condition('type','product')
        ->sort('created', 'DESC');

    $nids = $query->execute();
    $i = 0;

    foreach ($nids as $nid) {
        $node = NODE::load($nid);
            $imageurl[$i] = $node->get('field_product_page_display')->entity->getFileuri();
            $title[$i] = $node->get('field_title')->value;
            $imageurl[$i] = str_replace("public://", "", $imageurl[$i]);
            $i += 1;

        }
    return array(
        '#theme' => 'product_display',
        '#imageurl' => $imageurl,
        '#product_title' => $title,
        );
}

In my mymodule.module file I have

function mymodule_theme($existing, $type, $theme, $path){
    return array('header' =>
        array(
            'variables' => array(
                'header_title' => 'Default Title',
                'header_size' => 'h1',
                'text_align' => 'center',
                'uppercase' => FALSE,
                'grid_size' => 12,
                ), 
            ),
        'product_display' => 
            array(
                'variables' => array(
                    '#imageurl' => [],
                    '#product_title' => [],
                    ),
                ),
        );
}

in my product_display.html.twig file I have

{% for product in product_title %}
    {% for image in imageurl %}
         <div class="large-4 medium-3 column">
            <img src="./sites/default/files/{{ image }}">
                        <div class="product-title">
                            {{ product }}
                        </div>
                    </div> 
    {% endfor %}
{% endfor %}

I know for sure that my custom block.php file is able to retrieve the data properly because using Debug() on $imageurl or $title displays all the information. However using Dump() in product-display.html.twig shows the #imageurl and #product_title arrays as empty.

5

There is a syntax error in your hook_theme. Use this instead:

function mymodule_theme($existing, $type, $theme, $path){
return array('header' =>
    array(
        'variables' => array(
            'header_title' => 'Default Title',
            'header_size' => 'h1',
            'text_align' => 'center',
            'uppercase' => FALSE,
            'grid_size' => 12,
            ), 
        ),
    'product_display' => 
        array(
            'variables' => array(
                'imageurl' => [],
                'product_title' => [],
                ),
            ),
    );

}

The # sign in a render array is used to differenciate a key that has a meaning in the rendering versus an element. It shouldn't be included in the theme definition.

  • That is not a syntax error. – kiamlaluno Jul 7 '16 at 16:22
  • Well, in terms of php it is not. In terms of drupal, I think it is though because the number sign has a special meaning in the render arrays. It might work (not tested it) if the render array has a double number sign like '##imageurl' for variables since it has a number sign in the theme declaration, but I sure wouldn't recommend it as it might have an unexpected behavior. – yancg Jul 8 '16 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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