2

I've a block with values in the following array structure.

        $block['tag'][] = $term;
        $block['image'][] = $url;
        $block['title'][] = $title;
        $block['description'][] = $description;

From the function build in block, I'm passing it to theme as

$build = [
      '#theme' => 'allofus_four_grids_block',
      '#test_var' => $this->t('Test Value'),
      '#doubles' => array('key' => $block),
    ];

Then inside the module, I'm passing it to twig as

'allofus_four_grids_block' => [
      'variables' => [
        'block' => array('key' => array()),
      ],
      'path' => drupal_get_path('theme', 'basis') . '/templates/custom-blocks',
    ],

And in the twig I'm iterating it as

{% for key,value in block %}
    Key : {{ key }}
    Value : {{ value }}
    {% for key,text in value %}
        Key : {{ key }}
        Value : {{ text }}

    {% endfor %}

{% endfor %}

But this is not giving me the values?

1

Here you declare

'allofus_four_grids_block' => [
      'variables' => [
        'block' => array('key' => array()),
      ],
      'path' => drupal_get_path('theme', 'basis') . '/templates/custom-blocks',
    ],

So you need to use the variable block :

$build = [
      '#theme' => 'allofus_four_grids_block',
      '#test_var' => $this->t('Test Value'),
      '#block' => array('key' => $block),
    ];

Not #doubles

  • How do I get it in twig? – Fazeela Abu Zohra Dec 29 '16 at 4:03
  • 1
    you boucle like you do on block for the first level then another boucle for key – Kevin Dec 29 '16 at 7:11

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.