I've created a custom route that uses a controller to use a custom template. If I don't mess with base hook my template is rendered somewhere inside page.html.twig as a block and all the variables are accessible. However, I would like to design the page from scratch and be able to override the page.html.twig template. Is this possible?
I'm almost there but I can't access my variables in the template. And the twig dump() returns unstyled sfdump.
Here is the code:
LangController.php
<?php
namespace Drupal\te\Controller;
use Drupal\Core\Controller\ControllerBase;
class LangController extends ControllerBase{
public function langSingle() {
return [
'#theme' => 'page__lang_single',
'#lang_data' => 'testguk'
];
}
}
In the .module file I add more specific suggestions and implement the hook_theme()
mymodule.module
<?php
/**
* Implements hook_theme_suggestions_page_alter().
*/
function te_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if (\Drupal::routeMatch()->getRouteName() == 'te.lang_single') {
// Suggest a custom template for the page.
$suggestions[] = 'page__'.'lang_single';
}
}
function te_theme($existing, $type, $theme, $path) {
return [
'page__lang_single' => [
'base hook' => 'page',
'variables' => [
'lang_data' => NULL,
],
'template' => 'page--lang-single',
],
];
}
My template is in the module's templates folder. But if I hit my route I get this
What is strange the variables like page.navbar are accessible, but my expected lang_data is hardly accessible. Well at least I can't figure out how to render its value without usual dump.
If I search the page the value can be found, but there is definitelt something wrong with my template rendering
variables
key for a template that use abase hook
, because it's inherit of the ones of the base ( see documentation ). Maybe try to take this off, and if you have to add some variable do it with a preprocess hook.