1

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 %
1

I don't know why it was not working because after I've done a simple

<pre>
    {{ dump(myvariable) }}
</pre>

In order to check if the array was passed to the twig template, everything is working.

So the solution is in my-template.html.twig

{% for key, value in myvariable %}
    Key : {{ key }}<br>
    Date: {{ value.dateDebut }}<br>
    Societe : {{ value.societe }}<br>
{% endfor %}

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.