getSetting()'s documentation should specify what happens when a setting doesn't exist

Created on 20 October 2023, about 1 year ago
Updated 27 October 2023, about 1 year ago

Problem/Motivation

FieldStorageDefinitionInterface::getSetting() and DataDefinitionInterface() both have docs which say:

  /**
   * Returns the value of a given storage setting.
   *
   * @param string $setting_name
   *   The setting name.
   *
   * @return mixed
   *   The setting value.
   */

They don't say what happens if the setting name doesn't exist on the data being called. For example, if you have a text field and you do:

$text_field->getSetting('target_type'); // This setting does not exist on text fields and makes no sense!

However, the implementations return NULL in this case.

And there is apparently lots of code in the wild that relies on this behaviour, e.g. this in Commerce:

      $definitions = $this->entityFieldManager->getFieldDefinitions('commerce_product_variation', $variation_type_id);
      $definitions = array_filter($definitions, function ($definition) {
        /** @var \Drupal\Core\Field\FieldDefinitionInterface $definition */
        $field_type = $definition->getType();
        $target_type = $definition->getSetting('target_type');
        return $field_type == 'entity_reference' && $target_type == 'commerce_product_attribute_value';

This behaviour of the methods needs to be documented.

Steps to reproduce

Edit the file core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php and go to the getSetting() method.
Edit the file core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php and go to the getSetting() method.

Proposed resolution

Add documentation if the setting name doesn't exist to both method.

Remaining tasks

- Fix issue
- Community Review
- Commit

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Fixed

Version

10.1

Component
Documentation 

Last updated 1 day ago

No maintainer
Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024