2

I have two custom blocks that I created in files placed in the modules/custom/mymodule/src/Plugin/Block folder.

I defined two extra regions in the mytheme.info.yml file I placed the blocks in the regions using the 'Block layout' page in the admin area.

It's all working great on all pages that use the "normal" twig template naming conventions, such as page.html.twig, page--front.html.twig and other templates I created based on the suggestions in the comments twig debug outputs.

My problems started when I created templates via a Controller. Here is an example from my routing.yml file:

mytheme.bikes:
  path: '/catalog/Bikes'
  defaults:
    _controller: '\Drupal\mytheme\Controller\MyPageController::catalog_bikes'
    _title: 'Bikes'
  requirements: 
    _permission: 'access content'

And the controller:

public function catalog_bikes() {  
  return array(
    '#type' => 'page',
    '#theme' => 'page__bikes',
  );  
}   

If i go on mysite.com/catalog/Bikes the page--bikes.html.twig template is loaded, BUT, without the extra regions I added to mytheme.info.yml file

0

Are you rendering the regions in your twig template?

Like

{{ page.custom_region }}
  • yes I am doing exactly that – Liam Arbel Mar 23 '17 at 14:34
0

You've got a fundamental misunderstanding here on how the controller will work. When you return a response from a controller, it becomes the content on that page. You cannot assign the template for the page in your controller. If you want to use page--bikes.twig.html on that path, then you need to implement hook_theme_suggestions_HOOK_alter(), using the current path to determine the template suggestion(s):

hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)
{
  $current_path = \Drupal::service('path.current')->getPath()
  $path_args = explode('/', $current_path);
  // Check if we are on a catalog path, and that there is an
  // specific element on that page 
  if($path_args[0] == 'catalog' && isset($path_args[1]))
  {
    $suggestions[] = 'page__catalog__' . $path_args[1];
  }
}

This will allow you to use page--catalog--bikes.twig.html if you are on the /catalog/bikes page. I would suggest using 'catalog' as part of the template name, to prevent potential collisions with other modules.

  • I did what I did based on another thread I found on SE yesterday.. I tried what you suggested and while the template does load I still don't get the extra regions. – Liam Arbel Mar 23 '17 at 14:36

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.