diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 3885f3f..8bab7ef 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -983,4 +983,15 @@ public function referencedEntities() { return $referenced_entities; } + /** + * {@inheritdoc} + */ + public static function baseFieldDefinitionsByBundle(array &$field_definitions, $entity_type, $bundle) { + $node_type = node_type_load($bundle); + if (isset($node_type->title_label)) { + $field_definitions['title'] = clone $field_definitions['title']; + $field_definitions['title']->setLabel($node_type->title_label); + } + } + } diff --git a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php index 862b23f..0173b1e 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@ -62,6 +62,23 @@ public function initTranslation($langcode); public static function baseFieldDefinitions($entity_type); /** + * Alter base field definitions for the given bundle. + * + * @todo Document and/or provide a better DX for field overrides. + * + * @param string $entity_type + * The entity type to return properties for. Useful when a single class is + * used for multiple, possibly dynamic entity types. + * @param string $bundle + * Name of the bundle for which the overrides should be applied. + * @param \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions + * List of base fields for the given entity type. + * + * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions() + */ + public static function baseFieldDefinitionsByBundle(array &$field_definitions, $entity_type, $bundle); + + /** * Returns whether the entity has a field with the given name. * * @param string $field_name diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 455b18a..6e0a56f 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -396,7 +396,11 @@ protected function buildFieldDefinitions($entity_type, $bundle) { } $field_definitions = $this->baseFieldDefinitions[$entity_type]; - // @todo: Allow to alter the base fields by bundle. + // When we build fields for a specific bundle, allow the entity class to + // override the base fields. + if ($bundle) { + $class::baseFieldDefinitionsByBundle($field_definitions, $entity_type, $bundle); + } // Invoke hooks. $result = $this->moduleHandler->invokeAll($entity_type . '_field_info', array($bundle)); diff --git a/core/modules/node/lib/Drupal/node/Entity/Node.php b/core/modules/node/lib/Drupal/node/Entity/Node.php index fee4c18..daecae1 100644 --- a/core/modules/node/lib/Drupal/node/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Entity/Node.php @@ -375,7 +375,6 @@ public static function baseFieldDefinitions($entity_type) { $fields['title'] = FieldDefinition::create('text') ->setLabel(t('Title')) ->setDescription(t('The title of this node, always treated as non-markup plain text.')) - ->setClass('\Drupal\node\NodeTitleItemList') ->setRequired(TRUE) ->setTranslatable(TRUE) ->setSettings(array( diff --git a/core/modules/node/lib/Drupal/node/NodeTitleItemList.php b/core/modules/node/lib/Drupal/node/NodeTitleItemList.php deleted file mode 100644 index 2b81193..0000000 --- a/core/modules/node/lib/Drupal/node/NodeTitleItemList.php +++ /dev/null @@ -1,34 +0,0 @@ -getType()); - if (isset($node_type->title_label)) { - $definition->setLabel($node_type->title_label); - } - parent::__construct($definition, $name, $node); - } - -}