No field wrapper generated

Created on 27 March 2023, over 1 year ago
Updated 6 June 2023, over 1 year ago

Problem/Motivation

There is no field wrapper around a field using an In Other Words field formatter. This causes issues for styling the field.

Steps to reproduce

  1. Configure a field to use an In Other Words field formatter
  2. View the source code of a node using that field.

Remaining tasks

  1. Fix the bug
  2. Write test coverage
  3. Get maintainer approval

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO

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

Comments & Activities

  • Issue created by @cedewey
  • πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

    This is a Drupal-caused bug in my estimation. We extend (for example) EntityReferenceLabelFormatter which extends EntityReferenceFormatterBase which extends FormatterBase, and that has a view function which we should not need to override which does something really weird:

      public function view(FieldItemListInterface $items, $langcode = NULL) {
    
        // If there are actual renderable children, use #theme => field, otherwise,
        // let access cacheability metadata pass through for correct bubbling.
        if (Element::children($elements)) {
          $entity = $items->getEntity();
          $entity_type = $entity->getEntityTypeId();
          $field_name = $this->fieldDefinition->getName();
          $info = [
            '#theme' => 'field',
            '#title' => $this->fieldDefinition->getLabel(),
            '#label_display' => $this->label,
            '#view_mode' => $this->viewMode,
            '#language' => $items->getLangcode(),
            '#field_name' => $field_name,
            '#field_type' => $this->fieldDefinition->getType(),
            '#field_translatable' => $this->fieldDefinition->isTranslatable(),
            '#entity_type' => $entity_type,
            '#bundle' => $entity->bundle(),
            '#object' => $entity,
            '#items' => $items,
            '#formatter' => $this->getPluginId(),
            '#is_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(),
            '#third_party_settings' => $this->getThirdPartySettings(),
          ];
    
          $elements = array_merge($info, $elements);
        }
    
        return $elements;
      }
    

    That's a barely truncated excerpt, but you can see the weirdness. Why would what we return from viewElements, so long as it is present and renderable at all, which what we return most definitely is, affect whether the field wrapper and field label get applied?

    • mlncn β†’ committed d2ab4b8e on 3.0.x
      Provide an explicitly zero-indexed array so field theme added...
  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO

    Confirmed that the fix worked. There is now a field wrapper there. Thanks!

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO

    Spoke too soon.

    The fix holds for term reference fields, but is still present for List (string, integer and float) fields.

    • mlncn β†’ committed ebf9f682 on 3.0.x
      Apply fix for getting field wrapper/label to all formatters
      
      Issue #...
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

    Yes! There's List formatter which is one of the ones applying to terms (and any entity reference), and then there is also Sequential terms formatter and also OptionsList which applies to text type lists so your observations hold (don't blame me those names are from core).

    Sorry i tend to stop thinking once the fix is identified, who knew you also needed to apply it.

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO

    Confirmed this is now working for all fields and field formatters that In Other Words is compatible with. Thanks!

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO
Production build 0.71.5 2024