Problem/Motivation
Hardening: guard getFormatterType() callsites + normalize field_types to array
If a formatter config entity exists with an unknown/missing type (or partially imported/broken config), several call sites assume getFormatterType() always returns a valid plugin instance. In reality it can return FALSE, and that can produce fatals (calling methods on FALSE) during
Additionally, the derivative plugin definition should guarantee field_types is always an array. If it becomes NULL, Drupal core field formatter selection logic can throw a TypeError.
Affected areas (examples)
- Deriver should ensure field_types is always an array:
CustomFormatters.php - Form save should not blindly call submitForm() on a missing formatter type:
FormatterForm.php - Field formatter rendering should handle missing entity/type gracefully:
CustomFormatters.php - Entity lifecycle methods should guard plugin calls and normalize field_types:
Formatter.php
Proposed resolution
- Normalize
field_typesto an array in the deriver (CustomFormatters.php). - Guard
getFormatterType()before calling plugin methods in:src/Entity/Formatter.php(preSave/postLoad/calculateDependencies)FormatterForm.php(save)CustomFormatters.php(rendering)
- In the deriver, normalize field_types:
- if it’s NULL →
[] - if it’s scalar →
[$field_types]
- if it’s NULL →
- Guard all
getFormatterType()->… callsites:- assign to
$formatter_type = $entity->getFormatterType(); - only call methods if
$formatter_typeis truthy
- assign to
- In entity methods, avoid using dynamic properties (PHP 8.2+); read/write via
$this->get('field_types')and$this->set('field_types', $normalized).
This will prevent hard failures and keeps Drupal UI usable even when configuration is temporarily inconsistent.
Issue fork custom_formatters-3572918
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
nickolajPatch guards all `getFormatterType()` call sites against FALSE return values, normalizes `field_types` to always be an array in the deriver, and replaces direct `$this->field_types` property access with `$this->get('field_types')`/`$this->set()` for PHP 8.2+ compatibility.
Comment #5
decipheredThanks Mykola. Committed to 4.1.x