Ensure selected field formatters are expected and supported by our twig entity templates

Created on 13 September 2022, over 2 years ago
Updated 25 April 2024, 10 months ago

Problem/Motivation

TL;DR:
If we're accessing field values directly in an entity template (which is a different level and where ever possible should delegate that to the field), we at least have to ensure the preconditions are met, otherwise fall back to the default and log an error or throw a hard error.

The full story:
In our templates, for example for media documents, we expect certain structures (array / object key / values) from the selected field formatters.
As formatters are Drupals way to preprocess the field output before rendering, partially with important modifications, up to security relevant features, we should not simply skip them. Fetching the values from the entity directly may look like a reliable solution, but completely leaves out the important formatter logic.
There are several file / media download modules for Drupal, for different use cases, which we'd skip with unexpected results, if grabbing the value directly from the entity, so this is no option.

The real issue
If an unsupported formatter is selected, the template is unable to handle its unknown structures correctly.
For example, the following line is such a case:
{{ link(link_label, content.field_document.0['#url'], media_link_attributes.addClass(link_classes)) }}
https://git.drupalcode.org/project/drowl_media/-/blob/3.x/modules/drowl_...

Steps to reproduce

In a media document display (e.g. admin/structure/media/manage/document/display), for example, select a file field formatter which isn't supported by our module, like file_url_plain where we expect a file_download_link formatter.

Proposed resolution

The most clean way to solve this, would be to lock all the entity displays (e.g. media entity document) formatters to the formatters we expect. But as there's no technical solution for this in Drupal and in special cases we may even want to change that setting, this is not an option, but would guarantee the provided values are always as expected.

Proposed solution
A good solution here should be to be more strict and descriptive about the formatters we expect and support in our template logic!
In the past we made the mistake to not define the expected formatter or at least expected data structure.

This is bad, because it leads to errors which don't point to the key issue (unsupported formatter selected). Instead errors like missing keys or values are thrown, which requires a lot of debugging to find the real issue (unexpected formatter).

So we need to list the supported formatters and
a) fall back to the default output
or
b) Throw an exception if an unsupported formatter is selected for a relevant field

Alternative solution:
Another clean way to solve this, but at the same time presumably with a lot more complexity, would be to not solve this at the level of the media entity, but at the media entities' field level. Due to the added complexity, I guess we'll decide against that, but it should be mentioned, that our implementation pulls kind of internal data from fields into a different (the entity template) level.

Remaining tasks

  1. Prepare a list of all affected twig files in this module.
  2. Provide a section in the head of the twig files to define and check for the supported formatters
  3. Put variables differing based on the formatter into a consistent variable, e.g. "link_uri" to use it consistently below
  4. Otherwise fall back to default entity representation: {{ content }}
  5. Test
  6. Release

User interface changes

None

API changes

None

Data model changes

None

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

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
  • 🇩🇪Germany Anybody Porta Westfalica

    @thomas.frobieter as this is related to field values and defaults, does it make sense to re-read the issue now and decide when and how to proceed? (As of the Provide list_string field values and defaults programmatically Active implementation)

  • Not sure why we should bloat the templates with this while we have locked the field settings. So.. when someone fucks up the field configuration its PEBKAC?

  • 🇩🇪Germany Anybody Porta Westfalica

    The case where we ran into this was, that a formatter was chosen that is not supported. This can happen, if we use further custom formatters in a project for some reason or someone else beside you picked the formatter (e.g. me or another developer) - I don't think it will typically be a customer.

    The goal is to make this visible in cases where the template makes special assumptions on the data structure of the values.
    As written in the issue summary, there's sadly no way to lock the field displays.

    Btw I think this kind of slang language ("fucks up") doesn't fit here and isn't good for your personal and our company reputation in public. There are similar, better words to choose for an educated individual whatever your mood is. 😉

  • All right.. I forgot that the display settings aren't locked. So this makes totally sense.

    However, this is unrelated to the other issues and will take some hours. I won't fix it now.

    Btw I think this kind of slang language ("fucks up") doesn't fit here and isn't good for your personal and our company reputation in public. There are similar, better words to choose for an educated individual whatever your mood is. 😉

    Not sure whats the problem. Its slang, but it doesn't mean anything bad and nobody can feel offended by it.

Production build 0.71.5 2024