1

I am sending data from a custom module controller to a template file using the following code.

module.info.yml

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module

function module_theme($existing, $type, $theme, $path) {
  return [
    'module_template' => [
      'variables' => [
        'var' => [],
        'title' => ''
      ]
    ]
  ];
}

module.routing.yml

module.event:
  path: '/path/{dynamic}'
  defaults:
    _controller: '\Drupal\module\controller::RunQuery'
    _title: 'Module'
  requirements:
    _permission: 'access content'

module-template.html.twig

<p> test {{ title }}</p>
<ul>
{% for user in var %}
  <li> {{ user.title }} </li>
{% endfor %}
</ul>
test 2

controller.php

namespace Drupal\module;

class controller {

  public function RunQuery($dynamic) {

    // Some query here which work fine and return what I want
    $ok = 'Some value';

    return [
      '#theme' => 'module_template',
      '#var'   => $ok,
      '#title' => 'Title',
    ];
  }

}

This will return only test and test 2 in the template file, so the template file is called by the module controller, but it does not show {{ title }} and {{ user.title }}.

1 Answer 1

1

I don't know what exactly you are trying to do as var never seems to be passed an array or object but I'll give you a working example. Your controller should extend ControllerBase and use public function content() to output stuff. Also pay attention to reference the correct namespace and pay attention to upper and lower case writing as well.

module.info.yml

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module

function module_theme($existing, $type, $theme, $path) {
  return [
    'module_template' => [
      'variables' => [
        'var'   => NULL,
        'title' => NULL,
      ],
    ],
  ];
}

module.routing.yml

module.event:
  path: '/path/{user}'
  defaults:
    _controller: '\Drupal\module\Controller\Controller::content'
    _title: 'Module'
  requirements:
    _permission: 'access content'

templates/module-template.html.twig

<p> test {{ title }}</p>
<div>{{ var.getUsername }}</div>
test 2

src/Controller/Controller.php

namespace Drupal\module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;

class Controller extends ControllerBase {

  public function content(AccountInterface $user) {

    return [
      '#theme' => 'module_template',
      '#var'   => $user,
      '#title' => 'Title',
    ];
  }

}

Now you can access /path/1 or whatever user ID you want to get this user's name printed.

With the Devel sub-module Kint installed you can use {{ kint() }} or {{ kint(var) }} inside the template to check out what else you can get and print.

Your Answer

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

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