Dimensions render element / field widget styling lost when used with Claro

Created on 16 July 2021, over 3 years ago
Updated 3 September 2024, 2 months ago

Problem/Motivation

While Claro is currently still experimental, it will eventually become the new default admin theme.

The field widget for a "Dimensions" field (DimensionsDefaultWidget) delegates to the render element \Drupal\physical\Element\Dimensions.

The processElement() method uses #field_suffix to add in a Γ— separator, and makes the field label for Units invisible.
Which makes sense when viewed in Seven:

But without those theme-specific CSS floats, here it is in Claro:

Steps to reproduce

Add a Dimensions field to any content type
Change the admin theme to Claro

Proposed resolution

Options:

  1. Write custom CSS to restore the floats
  2. Drop the field suffixes and restore the label for Units

Also consider making this optional? And controllable from the render array (and ideally even from the field widget!)

Remaining tasks

Decide on an approach

User interface changes

Depends on the approach

API changes

N/A

Data model changes

N/A

πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States tim.plunkett Philadelphia

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.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @captain hindsight could you prepare this as MR perhaps? So you're suggesting to add form-items-inline class?

    The existing MR seems outdated and needs a rebase.

  • Status changed to Needs work 3 months ago
  • πŸ‡©πŸ‡ͺGermany Grevil

    @captain hindsight this would be the same, as putting the attributes in the info directly:

      /**
       * {@inheritdoc}
       */
      public function getInfo() {
        $class = get_class($this);
        return [
          // All units are available by default.
          '#available_units' => [],
    
          '#size' => 10,
          '#maxlength' => 128,
          '#default_value' => NULL,
          '#attached' => [
            'library' => ['physical/admin'],
          ],
          '#process' => [
            [$class, 'processElement'],
            [$class, 'processAjaxForm'],
            [$class, 'processGroup'],
          ],
          '#pre_render' => [
            [$class, 'preRenderGroup'],
          ],
          '#input' => TRUE,
          '#theme_wrappers' => ['fieldset'],
          '#attributes' => [
            'class' => [
              'form-items-inline',
            ],
          ],
        ];
      }
    

    Unfortunately, that will only add the class to the fieldset, instead of adding it to the underlying "fieldset_wrapper":

    We tried putting the class into "#content_attributes" as well as "#wrapper_attributes", but that won't put the class into the fieldset wrapper unfortunately.

  • πŸ‡©πŸ‡ͺGermany captain hindsight

    Hm OK, I see I was talking about the physical_measurement element. For this my approach works. Unfortunately I don't use the physical_dimensions element, but yes it might not work for that if you add it to attributes.

    Claro supports on the other hand content_attributes for its wrapper, I think you could achieve the same if you add `form-items-inline` to content_attributes for web/core/themes/claro/templates/fieldset.html.twig:

    <fieldset{{ attributes.addClass(classes) }}>
      {#  Always wrap fieldset legends in a <span> for CSS positioning. #}
      {% if legend.title %}
      <legend{{ legend.attributes.addClass(legend_classes) }}>
        <span{{ legend_span.attributes.addClass(legend_span_classes) }}>{{ legend.title }}</span>
      </legend>
      {% endif %}
    
      <div{{ content_attributes.addClass(wrapper_classes) }}>
    

    At least it should work if you do this in a `hook_preprocess_fieldset`...

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thanks @captain hindsight. Indeed https://www.drupal.org/node/2066209 β†’ looks interesting @grevil...

  • πŸ‡©πŸ‡ͺGermany Grevil

    Can't seem to get it to work. I tried both:

    [...]
          '#theme_wrappers' => [
            'fieldset' => ['#wrapper_attributes' => ['class' => ['form-items-inline']]],
          ],
        ];
      }
    

    and

    [...]
          '#theme_wrappers' => [
            'fieldset' => ['#attributes' => ['class' => ['form-items-inline']]],
          ],
        ];
      }
    
  • πŸ‡©πŸ‡ͺGermany Grevil

    Ok, I found the reason we can't seem to override anything through πŸ› Field (widget) descriptions are displayed wrong (should not be in .form-item__suffix) Needs work .

    The child element used in Dimensions (and Measurement as well) has its own "form_element" theme wrapper. And because we are rendering the child element in a foreach, we basically add 3 div wrappers around each input, which results in the current look.

    I tried to simply remove the child element theme wrapper inside the parent class, but this seems to not be easily done. I am also not quite sure, why "Number" (physical_number) has a form_element wrapper, as it seems to be used internally only and not as a standalone widget.

    The only problem is, that if we would simply remove the "form_element" theme wrapper from the child element, we would:

    • A. Introduce a breaking change, as "physical_number" can't have a description nor label anymore
    • B. "physical_number" won't support "#field_suffix" anymore, so we would need another way on how to render the unit field suffix / the "x" between the input fields.

    This is how it looks, when simply removing the child element theme wrapper and "User cannot modify the unit of measurement":

    As you can see the measurement and "x" is missing, because "#field_suffix" isn't supported anymore by the child element.

Production build 0.71.5 2024