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
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:
- 3395748-getsettings-documentation
changes, plain diff MR !5126
Comments
Comment #2
karimb commentedI'm going to take this issue as part of the Symetris' novice contribution workshop.
Comment #4
karimb commentedComment #5
karimb commentedComment #6
joachim commentedLGTM! Thanks!
Comment #11
lauriiiCommitted d088a7b and pushed to 11.x. Cherry-picked all the way to 10.1.x since this is a documentation improvement. Thanks!