To support different field instances per bundle and base fields that differ per bundle (like the configurable node title label), the API for fetching and defining entity fields has been changed.
As part of the method renames, terminology was also slightly changed: base fields are all entity fields that exist independently of the bundle, not just fields defined in $EntityClass::baseFieldDefinitions().
The following changes were done:
Entity class methods
Changed: $EntityClass::baseFieldDefinitions($entity_type_id) to $EntityClass::baseFieldDefinitions(EntityTypeInterface $entity_type)
New: $EntityClass::bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions)
Hooks
Renamed: hook_entity_field_info($entity_type) to hook_entity_base_field_info(EntityTypeInterface $entity_type)
Renamed: hook_entity_field_info_alter(&$info, $entity_type) to hook_entity_base_field_info_alter(&$base_field_definitions, EntityTypeInterface $entity_type)
New: hook_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions)
New: hook_entity_bundle_field_info_alter(&$field_definitions, EntityTypeInterface $entity_type, $bundle)
The bundle for EntityManager::getFieldDefinitions($entity_type_id, $bundle) is now mandatory. EntityManager::getBaseFieldDefinitions($entity_type_id) has been added to allow to fetch the base field definitions. This makes it clear that the common use case is to specify the entity type and bundle.
To provide an alteration of a base field for a specific bundle, clone the base field and then make the desired changes. Only return new and changed fields.
/**
* {@inheritdoc}
*/
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
$node_type = node_type_load($bundle);
$fields = array();
if (isset($node_type->title_label)) {
$fields['title'] = clone $base_field_definitions['title'];
$fields['title']->setLabel($node_type->title_label);
}
return $fields;
}