diff --git a/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php b/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php index bc8f9c0a2b9ccb167376bd47f3166ef3c04d4113..2604eec50270a07016f37435dbd73ab4b1965946 100644 --- a/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php +++ b/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php @@ -87,4 +87,19 @@ public function setFieldDefinition($field_definition) { 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 3ce62f243d0d036f5a596a4eb035bebeadb31e4d..140230c21f61aa480c7bd5a121050d432c32871e 100644 --- a/core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php +++ b/core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php @@ -99,4 +99,16 @@ protected function assertSavedFieldItemValue(EntityTest $entity, string $expecte $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()); + } + }