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_types to 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]
    • Guard all getFormatterType()->… callsites:
      • assign to $formatter_type = $entity->getFormatterType();
      • only call methods if $formatter_type is truthy
    • 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.

Command icon 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

mbuechner created an issue. See original summary.

nickolaj made their first commit to this issue’s fork.

nickolaj’s picture

Status: Active » Needs review

Patch 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.

deciphered’s picture

Version: 4.1.0-beta1 » 4.1.x-dev
Status: Needs review » Fixed

Thanks Mykola. Committed to 4.1.x

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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