Problem/Motivation
I created a custom layout using the annotations method from the docs. I since I had multiple classes that would use the same template I decided to use the hook_theme method of rendering instead. Once I replaced template = "templates/advanced-layout-4" and library = "my_custom_module/advanced-layout-library" with theme_hook: "custom_layout" I lost the classes that were in my twig template.
After some debugging I realized when using the hook_theme method for rendering the region_attributes class in my twig template was set to null. I'm not sure if this is expected behavior, but it seems like a bug.
What are the steps required to reproduce the bug?
- Follow steps using the annotations method from the docs
- create hook_theme call in my_module and create hook based on hook_theme doc.
- replace
template = "templates/advanced-layout-1"andlibrary = "my_custom_module/advanced-layout-library"in class annotations withtheme_hook: "advanced_layout_1"
What behavior were you expecting?/What happened instead?
I was expecting for the region_attributes.main.addClass('main-region') function to append my class to the div, but after switching to hook_theme this stop happening, because region_attributes was returning null.
Code Sample
my_module.module
<?php
function scad_services_theme() {
return [
'bootstrap_sidebars_n' => [
'template' => 'advanced-layout-4',
'render element' => 'content',
],
];
}
\Plugin\Layout\Bootstrap.php.
?php
namespace Drupal\my_module\Plugin\Layout;
use Drupal\Core\Layout\LayoutDefault;
/**
* A very advanced custom layout.
*
* @Layout(
* id = "bootstrap_test",
* label = @Translation("Sidebar Layout Test"),
* category = @Translation("Ours"),
* theme_hook = "bootstrap_sidebars_n",
* regions = {
* "sidebar" = {
* "label" = @Translation("Main content"),
* },
* "row_1_c_1" = {
* "label" = @Translation("Column 1"),
* },
* "row_1_c_2" = {
* "label" = @Translation("Column 2"),
* },
* "row_1_c_3" = {
* "label" = @Translation("Column 3"),
* },
* "row_2_c_1" = {
* "label" = @Translation("Column 1"),
* },
* "row_2_c_2" = {
* "label" = @Translation("Column 2"),
* },
* "row_2_c_3" = {
* "label" = @Translation("Column 3"),
* },
* }
* )
*/
class Bootstrap extends LayoutDefault {
// Override any methods you'd like to customize here!
}
Comments
Comment #2
tim.plunkettIt seems that
template_preprocess_layout()is not running. Can you confirm that it does run for other layouts but not the one you are describing?Comment #3
jalite1991 commented@tim.plunkett I'm not sure what you mean by "other layouts". This is me attempting to create a custom layout using the theme_hook method rather than providing a template path. The only other layouts available are core's one_col, two_col, three_col, and four_col. From what I can tell they seem to just use the template path method.
So to answer your question, Yes these other templates do receive the region_attributes variable in their twig files. But I don't have trouble when I switch the back to template path method. Its only when I used the hook_theme method that the region_attributes seems to return null.
Because that variable is null the drag and drop functionality no longer works on the edit page.
Comment #4
tim.plunketttemplate_preprocess_layout()is the core function that sets up theregion_attributesvariable.I'm trying to be understand if that is not running for your custom layout.
Comment #6
tim.plunkettWaiting on an answer to #2/#4
Comment #10
smustgrave commentedSince this was moved to PNMI 2 years ago waiting on answers for #2 and #4 without an update going to close out.
If you still feel it's an issue please feel free to reopen.