Custom Style Plugin don't work as build() method is never called

Created on 4 July 2022, over 2 years ago
Updated 1 October 2024, 3 months ago

Problem/Motivation

I tried creating a custom Style plugin but it's not working because the build() method is never called. This is happening both using layouts (bootstrap_layout_builder) and blocks (layout_builder_blocks).

I've managed to trace it down to when Drupal\bootstrap_styles\StylesGroup::buildStyles is called:

  public function buildStyles(array $build, array $plugins_storage, $theme_wrapper = NULL) {

I can see that $plugins_storage is never listing my custom plugin, so it won't be instantiated and therefore its build() method won't be called.

$plugins_storage comes from Drupal\bootstrap_layout_builder\Plugin\Layout\BootstrapLayout::build(), exactly from $this->configuration['container_wrapper']['bootstrap_styles']. This configuration is not taking into consideration the new plugin.

      // Build dynamic styles.
      $build = $this->stylesGroupManager->buildStyles(
        $build,
      // storage.
        $this->configuration['container_wrapper']['bootstrap_styles'],
      // Theme wrapper that we need to apply styles to it.
        'blb_container_wrapper'
      );

Tests and additional info:

-> Caches have been cleared
-> I have tested both on already existing layouts/blocks and creating brand new layouts/blocks.
-> My custom plugin is a copy of TextAlignment plugin, just changing the machine names.
-> The rest of the plugin works as expected: values can be introduced and saved, it's listed on the config pages, can be enabled and disabled, forms appear and its data is saved.

Steps to reproduce

Create a Custom Style plugin, in this case a copy of \Drupal\bootstrap_styles\Plugin\BootstrapStyles\Style\TextAlignment, on a custom module.

Enable the module, enable the plugin on (for layouts) '/admin/config/bootstrap-layout-builder/styles' or (for blocks) '/admin/config/layout-builder-blocks/styles'.

Configure the new plugin classes on '/admin/config/bootstrap-styles/settings'. Save them.

Use the style on a layout section or block using one of the modules that implement bootstrap_styles (layout_builder_blocks or bootstrap_layout_builder).

The value will be saved but the class won't be added to the block/section as the build() method is never called.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Closed: works as designed

Version

1.0

Component

Code

Created by

🇪🇸Spain idiaz.roncero Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom marcosdr

    For anyone else encountering this issue, this problem happened to me when the annotated style id does not match the key of the returned array from the submitStyleFormElements() method.

    /**
     * @Style(
     *   id = "my_plugin",
    */
    

    This id needs to match:

    public function submitStyleFormElements(array $group_elements) {
        return [
         'my_plugin' => [
            'value' => 'whatever'
          ],
        ];
      }
    
Production build 0.71.5 2024