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

Issue fork drupal-3395748

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

joachim created an issue. See original summary.

karimb’s picture

I'm going to take this issue as part of the Symetris' novice contribution workshop.

karimb’s picture

Issue summary: View changes
karimb’s picture

Status: Active » Needs review
joachim’s picture

Status: Needs review » Reviewed & tested by the community

LGTM! Thanks!

  • lauriii committed d088a7b0 on 11.x
    Issue #3395748 by KarimB, joachim: getSetting()'s documentation should...

  • lauriii committed 7489ccac on 10.2.x
    Issue #3395748 by KarimB, joachim: getSetting()'s documentation should...

  • lauriii committed 6a68463c on 10.1.x
    Issue #3395748 by KarimB, joachim: getSetting()'s documentation should...
lauriii’s picture

Version: 11.x-dev » 10.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed d088a7b and pushed to 11.x. Cherry-picked all the way to 10.1.x since this is a documentation improvement. Thanks!

Status: Fixed » Closed (fixed)

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