diff --git a/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php b/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php index bc8f9c0a2b9..2604eec5027 100644 --- a/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php +++ b/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php @@ -87,4 +87,19 @@ class FieldItemDataDefinition extends DataDefinition implements FieldItemDataDef return $this; } + /** + * Gets the label of the field type. + * + * If the label hasn't been set, then fall back to the label of the + * typed data definition. + * + * @return string + * The label of the field type. + * + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + public function getLabel() { + return parent::getLabel() ?: $this->getTypedDataManager()->getDefinition($this->getDataType())['label']; + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php b/core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php index 3ce62f243d0..140230c21f6 100644 --- a/core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php +++ b/core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php @@ -99,4 +99,16 @@ class FieldItemTest extends EntityKernelTestBase { $this->assertEquals($expected_value, $entity->{$this->fieldName}->value); } + /** + * Tests \Drupal\Core\Field\TypedData\FieldItemDataDefinition::getLabel(). + */ + public function testGetLabel(): void { + $data_definition = \Drupal::service('typed_data_manager')->createDataDefinition('field_item:string'); + $this->assertEquals('Text (plain)', $data_definition->getLabel()); + + $label = 'Foo bar'; + $data_definition->setLabel($label); + $this->assertEquals($label, $data_definition->getLabel()); + } + }