Field formatter with inline settings is missing field formatters from the referenced (media) entity type

Created on 20 February 2025, 7 months ago

Problem/Motivation

When using the "Field formatter with inline settings" some field formatters that are available on the field display of the referenced media entity type itself are not available through "Field formatter with inline settings".

Steps to reproduce

1. Add a media video reference field on any node type (Media type "Video")
2. Configure the field display formatter to use "Field formatter with inline settings", choose field name "Video file (field_media_video_file")
3. Click update and save
4. Again: Edit the formatter and check the available formatters:

5. Go to the media video display settings (/admin/structure/media/manage/video/display) and check the availble formatters on the "Video file (field_media_video_file)" field. For example, we have a "Video" formatter their, which is missing on the Media reference field with the "Field formatter with inline settings":

Or: Install the https://www.drupal.org/project/plyr module, same problem here: The display formatter "Plyr for video files" is only available on the media entity type itself, but not through the "Field formatter with inline settings"

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

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

Merge Requests

Comments & Activities

  • Issue created by @thomas.frobieter
  • 🇩🇪Germany Grevil

    I will have a deeper look. This is caused by how the apllicable formatters are loaded. For each formatter "isApplicable()" is called, but both the Vidstack and Video formatter are extending the core "FileMediaFormatterBase". And their implementation of "isApplicable" fails, because the doesn't apply here, because the "FieldFormatterWithInlineSettings" field definition "file_extensions" setting doesn't include the media mime type.

  • 🇩🇪Germany Grevil

    Ok, I found the issue.

    Instead of using the actual "real" field defintion of the referenced entity, we create a new one from the given storage definition here.

    In the case of the file field, this will lead to "$field_definition->getSetting('file_extensions')" having the default value: "txt", instead of the actual "mp4" value. This in turn, leads to "isApplicable" returning false and the formatter not being displayed.

  • 🇩🇪Germany Grevil

    This should do the trick. Now we are getting the "real" field definition, instead of creating one.

  • Pipeline finished with Success
    7 months ago
    Total: 158s
    #441034
  • 🇩🇪Germany Grevil

    Just did some internal testing and seems to work great! 😁👍

  • Status changed to Needs review 2 months ago
  • I've applied MR18 on my local as i need a fix for 🐛 Field formatter with inline settings does not provide third_party_settings form of target field Needs work , but i'm getting the folowing warnings on the config form:

    Warning: Undefined array key "target_bundles" in Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterWithInlineSettings->settingsForm() (line 208 of modules/contrib/field_formatter/src/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettings.php).
    
    Warning: foreach() argument must be of type array|object, null given in Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterWithInlineSettings->settingsForm() (line 212 of modules/contrib/field_formatter/src/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettings.php).

    For context purposes, i'm using the formatter on a custom entity type w/o bundles. Looks like that case on not handled by the MR, the $fieldDefinition variable remains NULL if there are no `targetEntityBundleIds` entries.
    Core's `Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::defaultConfiguration()` comment on `target_bundles` (defaults to NULL) says:

    // For the 'target_bundles' setting, a NULL value is equivalent to "allow
     // entities from any bundle to be referenced" and an empty array value is
     // equivalent to "no entities from any bundle can be referenced".
Production build 0.71.5 2024