5 * Test module for the entity API providing a bundle field.
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\entity_test\FieldStorageDefinition;
11 use Drupal\entity_test\Entity\EntityTestMulRev;
14 * Implements hook_entity_type_alter().
16 function entity_schema_test_entity_type_alter(array &$entity_types) {
17 if (\Drupal::state()->get('entity_schema_update')) {
18 $entity_type = $entity_types['entity_test'];
19 $entity_type->set('translatable', TRUE);
20 $entity_type->set('data_table', 'entity_test_field_data');
21 $keys = $entity_type->getKeys();
22 $keys['revision'] = 'revision_id';
23 $entity_type->set('entity_keys', $keys);
28 * Implements hook_entity_base_field_info().
30 function entity_schema_test_entity_base_field_info(EntityTypeInterface $entity_type) {
31 if ($entity_type->id() == 'entity_test') {
32 $definitions['custom_base_field'] = BaseFieldDefinition::create('string')
33 ->setName('custom_base_field')
34 ->setLabel(t('A custom base field'));
35 if (\Drupal::state()->get('entity_schema_update')) {
36 $definitions += EntityTestMulRev::baseFieldDefinitions($entity_type);
37 // And add a revision log.
38 $definitions['revision_log'] = BaseFieldDefinition::create('string_long')
39 ->setLabel(t('Revision log message'))
40 ->setDescription(t('The log entry explaining the changes in this revision.'))
41 ->setRevisionable(TRUE);
48 * Implements hook_entity_field_storage_info().
50 function entity_schema_test_entity_field_storage_info(EntityTypeInterface $entity_type) {
51 if ($entity_type->id() == 'entity_test') {
52 $definitions['custom_bundle_field'] = FieldStorageDefinition::create('string')
53 ->setName('custom_bundle_field')
54 ->setLabel(t('A custom bundle field'))
55 ->setTargetEntityTypeId($entity_type->id());
61 * Implements hook_entity_bundle_field_info().
63 function entity_schema_test_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
64 if ($entity_type->id() == 'entity_test' && $bundle == 'custom') {
65 $definitions['custom_bundle_field'] = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type->id())['custom_bundle_field'];
71 * Implements hook_entity_bundle_create().
73 function entity_schema_test_entity_bundle_create($entity_type_id, $bundle) {
74 if ($entity_type_id == 'entity_test' && $bundle == 'custom') {
75 $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
76 $field_definitions = entity_schema_test_entity_bundle_field_info($entity_type, $bundle);
77 $field_definitions['custom_bundle_field']
78 ->setTargetEntityTypeId($entity_type_id)
79 ->setTargetBundle($bundle);
80 // Notify the entity storage that we just created a new field.
81 \Drupal::entityManager()->onFieldDefinitionCreate($field_definitions['custom_bundle_field']);
86 * Implements hook_entity_bundle_delete().
88 function entity_schema_test_entity_bundle_delete($entity_type_id, $bundle) {
89 if ($entity_type_id == 'entity_test' && $bundle == 'custom') {
90 $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
91 $field_definitions = entity_schema_test_entity_bundle_field_info($entity_type, $bundle);
92 $field_definitions['custom_bundle_field']
93 ->setTargetEntityTypeId($entity_type_id)
94 ->setTargetBundle($bundle);
95 // Notify the entity storage that our field is gone.
96 \Drupal::entityManager()->onFieldDefinitionDelete($field_definitions['custom_bundle_field']);
97 \Drupal::entityManager()->onFieldStorageDefinitionDelete($field_definitions['custom_bundle_field']);