Make attributes as plugins

Created on 23 February 2023, almost 2 years ago
Updated 21 March 2023, over 1 year ago

Thanks for this very valuable and stable module.

I am evaluating the possibility to use it on one of my project. As of now, it appears that the exposed fields are too "technical" from an editor standpoint.
I am wondering if it would be possible to review the architecture of the module so the fields are provided by plugins. It would allow custom modules to provides their own logic and form. The plugins using annotation would register for outer and/or title and/or inner elements. They would also register for ID or class or style or data- attribute. Plugins would also have a method which can decide if the plugin applies based on the context.

Lets say for example, I want to expose a "background color" field which technically would result in the addition of a class on the outer element of the block. This should be only available to a limited number of block types.

Here is some pseudo-code of the implementation of a plugin I have in mind.

/**
 * @LayoutBuilderComponentAttribute(
 *   id = "background_color",
 *   title = @Translation("Background color"),
 *   description = @Translation("Provide a predefined list of color to be used as the background color of the component."),
 *   attribute = "class",
 *   elements = ["outer"]
 * )
 */

...

public function applies($context) {
  $config = $configFactory->get('my_module.settings');
  if ($context['entity_type'] == 'block_content' && !in_array($context['bundle'], $config->get('bundles')) {
    return FALSE;
  }
}

public function buildForm(&$form) {
  $form['background_color'] = [
    '#type' => 'select',
    '#options' => [
      'black' => t('Black'),
      'white' => t('White'),
    ],
  ];
}

public function render(&$class) {
  return $class[] = 'bg-' . $this->configuration['background_color'] ?? 'white';
}

This module would obviously expose some basic plugins to cover the existing cases.

I understand that it is a massive change in the way the module currently work and that it would not be a shame to build a brand new module for it. However, I definitely think it makes sense to avoid having different modules for the same purpose which the only difference is the implementation.
I am not sure I will have time soon to work on that idea but I thought it was worth sharing while I have it in mind ;-)

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France vbouchet

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

Comments & Activities

  • Issue created by @vbouchet
  • πŸ‡ΊπŸ‡ΈUnited States Chris Burge

    @vbouchet As you mentioned, this feature request would require the module to be re-architected. Have you looked at the Layout Builder Styles β†’ module?

  • πŸ‡«πŸ‡·France vbouchet

    Hi @Chris. I found out the module after I started to re-architecture. Even if Layout Builder Styles is more flexible, I still found it restrictive (but I have not deep dive in the code yet). I will share a github repo with the approach to initiate the conversation.

  • πŸ‡«πŸ‡·France vbouchet

    I initiated this Github repository: https://github.com/vbouchet31/layout_builder_attributes

    While developing I realised that we can apply the same logic to section and region attributes, hence the module name is agnostic.
    Adding the possibility to create a plugin via a UI (similar to what Layout Builder Custom Styles does) should theoretically handle all the possible cases.

    Most basic cases are for editors to enter directly arbitrary class, id, style or data-* (achieved with default plugins).
    More advanced cases are for editors to pick human-friendly option from a predefined list which technically adds the class, id, style, data-* defined.
    Very custom cases (like the possibility for editors to only apply something on a "basic block" in the left-column of a 2 columns layout or the classes to be calculated at the rendering time based on the context) can be achieved via custom plugins.

Production build 0.71.5 2024