Verified Commit 973d283b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3128190 by phthlaap, Keshav Patel, msuthars, sja112, rlmumford,...

Issue #3128190 by phthlaap, Keshav Patel, msuthars, sja112, rlmumford, Prashant.c, smustgrave, quietone: FieldItemDataDefinition::getLabel() should show the label of the field type

(cherry picked from commit 94b03bcd)
parent c57ce025
Loading
Loading
Loading
Loading
Loading
+15 −0
Changes for core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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'];
  }

}
+12 −0
Changes for core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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());
  }

}