By berdir on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Description:
The default ContentEntityDatabaseStorage now supports data tables, which are used for making entity fields translatable.
To support translatable fields, an entity type just needs to define the data table in the entity type definition like this:
/**
* Defines the test entity class.
*
* @EntityType(
* id = "entity_test_mulrev",
* label = @Translation("Test entity - revisions and data table"),
* base_table = "entity_test_mulrev",
* data_table = "entity_test_mulrev_property_data",
* revision_table = "entity_test_mulrev_property_revision",
* )
*/
and flag the fields that are translatable and should be stored in that table accordingly in their baseFieldDefinitions() method of the storage controller:
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// ...
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the test entity.'))
->setTranslatable(TRUE)
->setSetting('max_length', 32)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
));
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The ID of the associated user.'))
->setSettings(array('target_type' => 'user'))
->setTranslatable(TRUE);
return $fields;
}
All fields that can vary per language (in the above example, the name and user id of the author) should be marked translatable.
Impacts:
Module developers