Passing an array stdClass Object from custom module to template in drupal 8

by Sébastien Gicquel   Last Updated June 12, 2019 16:07 PM

I’ve created some D8 custom modules which call rest API and and it’s working fine when I get simple array in my $response.

In my controller, this is working :

return [
            '#theme' => 'webservices_client',
            '#id' => $infos_clients->body->id,
            '#nom' => $infos_clients->body->nom,
 ];

But when I receive that kind of array $response->body :

Array
(
    [0] => stdClass Object
        (
            [numero] => 625
            [dateDebut] => 2018-09-27T09:21:54.5746973
        )

    [1] => stdClass Object
        (
            [numero] => 468
            [dateDebut] => 2018-05-17T16:50:33.6357383
        )

)

I don’t how to send my array from the controller to the twig template and assign variable in my-module.module

Could you explain how to this ?

I've tried this kind of thing :

in controller :

return [
            '#theme' => 'webservices_informations',
            '#myvariable' => $response,
       ];

in my-module.module :

function webservices_informations_theme(array $existing, $type, $theme, $path) {
    return array(
        'webservices_informations' => array(
            'variables' => array(
                'myvariable' => NULL,
            ),
        ),
    );
}

In twig template :

{% for key, value in myvariable %}
            Key : {{ key }}<br>
            Name : {{ value.id }}<br>
{% endfor %
Tags : 8 theming


Related Questions


Updated March 26, 2015 10:02 AM

Updated March 29, 2015 12:02 PM

Updated March 29, 2015 15:02 PM

Updated April 05, 2015 16:03 PM

Updated April 09, 2015 22:03 PM