Warning: Undefined array key "formatter_settings" in Drupal\custom_field\Plugin\Field\FieldFormatter\BaseFormatter->calculateDependencies()

Created on 14 November 2024, about 1 month ago

Problem/Motivation

This warning can popup in some cases when setting formatter display:
Warning: Undefined array key "formatter_settings" in Drupal\custom_field\Plugin\Field\FieldFormatter\BaseFormatter->calculateDependencies()

Which then leads to fatal type error:
TypeError: Drupal\custom_field\Plugin\CustomFieldFormatterBase::calculateFormatterDependencies(): Argument #1 ($settings) must be of type array, null given, called in /var/www/html/docroot/modules/contrib/custom_field/src/Plugin/Field/FieldFormatter/BaseFormatter.php on line 409 in Drupal\custom_field\Plugin\CustomFieldFormatterBase->calculateFormatterDependencies() (line 75 of /var/www/html/docroot/modules/contrib/custom_field/src/Plugin/CustomFieldFormatterBase.php).

Steps to reproduce

I triggered this by setting one of the formatter fields (boolean type) to hidden. The field did not yet have any formatter_settings saved in config.

Proposed resolution

Modify calculate dependencies to check existence of formatter settings.

/**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $fields = $this->getSetting('fields');
    $dependencies = parent::calculateDependencies();
    if (!empty($fields)) {
      foreach ($fields as $name => $field) {
        $plugin = $this->customFieldFormatterManager->createInstance($field['format_type']);
        if (method_exists($plugin, 'calculateFormatterDependencies')) {
          if (isset($field['formatter_settings'])) {
            $plugin_dependencies = $plugin->calculateFormatterDependencies($field['formatter_settings']);
            $dependencies = array_merge_recursive($dependencies, $plugin_dependencies);
          }
        }
      }
    }

    return $dependencies;
  }

Remaining tasks

Create patch.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇺🇸United States apmsooner

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024