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?

  1. Follow steps using the annotations method from the docs
  2. create hook_theme call in my_module and create hook based on hook_theme doc.
  3. replace template = "templates/advanced-layout-1" and library = "my_custom_module/advanced-layout-library" in class annotations with theme_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

Jalite1991 created an issue. See original summary.

tim.plunkett’s picture

Version: 8.8.4 » 8.8.x-dev
Issue tags: -hook_theme, -Layout Builder +Blocks-Layouts

It 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?

jalite1991’s picture

@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.

tim.plunkett’s picture

template_preprocess_layout() is the core function that sets up the region_attributes variable.
I'm trying to be understand if that is not running for your custom layout.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

tim.plunkett’s picture

Status: Active » Postponed (maintainer needs more info)

Waiting on an answer to #2/#4

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

Since 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.