0

I have a module that define a Routing and a controller

my.partner:
  path: '/partner/{alias}'
  defaults:
    _controller: '\Drupal\myname\Controller\PagesController::partner'
    _title: 'Partner'

In controller I define the template to use:

$variables = [
         '#theme' => 'page_partner',
]....

But I need to pass some "variables" from this "controller" to html <head>. How can I do it ?

1

Define the variables in your theme and the pass them as "#variable" = value. E.g. in YOUR_MODULE.module have something like:

function tb_megamenu_theme($existing, $type, $theme, $path) {
  $items['tb_megamenu'] = [
  'template' => 'tb-megamenu',
    'variables' => [
      'menu_name' => NULL,
      'content' => NULL,
      'section' => 'frontend',
      'block_theme' => \Drupal::config('system.theme')->get('default'),
    ],
  ];
}

In your controller use:

$render = [
  '#theme' => 'tb_megamenu',
  '#menu_name' => $this->getMenuName(),
  '#block_theme' => $this->getThemeName(),
  '#content' => $this->getContent(),
];
  • Sorry.. did not see the Head requirement. Clive is right. – CG Monroe Sep 26 '17 at 14:57
0

This has been bumped up to the homepage. To make the comment of @Clive more clear, you can attach page attachments via #attached to any theme or render element (#theme, #type, #markup) and it will bubble up to page level when rendered:

$variables = [
  '#theme' => 'page_partner',
  '#attached' => [
    'html_head' => [
      [
        [
          '#tag' => 'meta',
          '#attributes' => [
            'name' => 'foo',
            'content' => 'bar',
          ],
        ],
        'my_module_foo',
      ],
    ],
  ],
];

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.