[2.0.0-alpha3] Field Formatters : views, nesting & context handling

Created on 11 April 2024, 8 months ago
Updated 21 June 2024, 5 months ago

Problem/Motivation

When you choose to display a field in views, using a field formatter from ui_patterns, the entire list of source plugins are available for props and slots (without restricting the entity type to the entity type listed in the view, and without restricting the field to be the current field to be displayed). This bug is due to a problem in context matching between context injected to the source plugin manager, and the contexts declared in context_definition of all ui_pattern_field_formatters source plugins.

Indeed, what is special here is that, in the particular case, the display formatter settings form is called for the entity type and field name but without any bundle in the gathered "context" (there is no bundle by construction here).

Steps to reproduce

Enable UI patterns field formatters
Create views with field
Select ui patterns field formatter
Select a component
All props and slots, have lot of sources available by mistake as explained above.

Proposed resolution

  • Being able to tackle the situation where no bundle is available, with a small rework of ui patterns source plugin derivers and play with the context_definition of generated plugins.
  • Some source plugins have the actual context (entity_type_id, bundle, field_name) and they will not be used in field formatters inside views
  • Some source plugins won't have the bundle in context (there will be one plugin per entity type and field_name), so we would be able to find them in the context of field formatter inside views
  • This will require some modifications in the plugin deriver to be able to remove the bundle context, also add mechanism to restrict by bundle when you map inside display entity/layout builder case
  • Ui patterns field formatter version 1.0x : the context seems to be based on the entire field storage :
        $field_storage_definition = $this->fieldDefinition->getFieldStorageDefinition();
        $context = [
          'storageDefinition' => $field_storage_definition,
          'limit' => $field_storage_definition->getPropertyNames(),
        ];
        // Some modifications to make 'variant' default value working.
        $configuration = $this->getSettings();
    
        $this->buildPatternDisplayForm($form, 'field_properties', $context, $configuration);
    

Also we can explore the API of context provider to help to resolve this issue :
https://api.drupal.org/api/drupal/core%21modules%21node%21src%21ContextP...
https://drupalsun.com/philipnorton42/2023/01/22/drupal-10-creating-custo...

🐛 Bug report
Status

Fixed

Version

2.0

Component

UI Patterns Field Formatters [2.x only]

Created by

🇫🇷France musa.thomas France 🇫🇷

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

Merge Requests

Comments & Activities

  • Issue created by @musa.thomas
  • 🇫🇷France musa.thomas France 🇫🇷
  • 🇫🇷France musa.thomas France 🇫🇷
  • 🇫🇷France musa.thomas France 🇫🇷
  • First commit to issue fork.
  • Assigned to pdureau
  • Status changed to Needs review 7 months ago
  • Issue was unassigned.
  • Status changed to Needs work 7 months ago
  • 🇫🇷France pdureau Paris

    Tested with a node's title field:

    • For each: ❌ the form doesn't load
    • For all: ✅ We see only field properties now. According to Mikael, it is a generic "universal" system instead of just covering views' context.
  • Status changed to Needs review 7 months ago
  • 🇫🇷France just_like_good_vibes PARIS

    For each: ❌ the form doesn't load

    -> this is true when the field formatter "for each" is not applicable, when the cardinality of the storage is 1 at most.
    if you select a field with cardinality superior or equal to 2, the for each will work in views.

    i have checked the code, i am not 100% sure, but the list of formatters which is proposed in the views field option form, do not reflect the result of the method "isApplicable" inside the field formatters.

    according to what i have seen the line in class "Drupal\views\Plugin\views\field\EntityField" from views module.
    $formatters = $this->formatterPluginManager->getOptions($field->getType());

    so what do we do?

  • Status changed to Needs work 7 months ago
  • 🇫🇷France pdureau Paris

    A little update with my new batch of tests

    • "For all" on a single value field: ✅
    • "For all" on a multivalued field: ✅
    • "For each" on a single value field: ❌ the form doesn't load
    • "For each" on a multivalued field: ✅

    Before fixing "For each on a single value field" let's talk about those 2 formatters.

    Why do we have field properties source on "for all" ? It make sense when "For all" is applied to a single value field, but what is expected in a multivalued field?

    Proposal

    "Component (For all)" is renamed 'Component wrapper" (or something like that...) and has those sources:

    • Field label
    • Field formatter (with the full renderable)

    "Component (For each)"is simply renamed 'Component", is available for both single value and multivalued fields, a has those sources:

    • Field label
    • Field formatter (if the render array is a list, we take the corresponding rendered item; if the render array is a single renderable, what do we do? We return it or we reurn nothing?)
    • Field properties...

    Both are available for multivalues & single value fields.

  • 🇫🇷France just_like_good_vibes PARIS

    About for each and for All,
    why not just use the terms "Components"
    (for each), and "Component"?

  • 🇫🇷France just_like_good_vibes PARIS

    i take this one

  • Status changed to Needs review 6 months ago
  • 🇫🇷France just_like_good_vibes PARIS

    Hello,
    as we discussed during the camp, i made the changes to allow better field formatter naming and working.
    i think this MR is one of the toughest i have made during the last weeks, but it brings so much goodness :

    + better source plugins management for slots and props, through a better handling of contexts. Especially custom contexts which are
    specified by ui patterns sources and have constraints (views source plugins, field prop source plugins...etc). The magic really happens now
    The modification allowed also to clean the ".module" of ui_patterns_views and put the right code at the right place inside each ui patterns views ' source plugins ;)

    + some bugfixes on ui_patterns_field_formatters, which were appearing during nesting. the nested field formatter was not handling correction the field definition..

    + some renaming of the two field formatters which are "Single component" and "Component per item", to allow better understanding of what's happening when you render a field and its items with ui patterns.

    + renaming of field formatter PHP classes for better homogeneity

    please review

  • Issue was unassigned.
  • Status changed to Fixed 6 months ago
  • 🇫🇷France pdureau Paris

    Merged

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024