50bf3e3bb1e49643c3b5903aecac7d6dec0c44ed
[yaffs-website] / web / core / modules / system / tests / modules / entity_schema_test / entity_schema_test.module
1 <?php
2
3 /**
4  * @file
5  * Test module for the entity API providing a bundle field.
6  */
7
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;
12
13 /**
14  * Implements hook_entity_type_alter().
15  */
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);
24   }
25 }
26
27 /**
28  * Implements hook_entity_base_field_info().
29  */
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);
42     }
43     return $definitions;
44   }
45 }
46
47 /**
48  * Implements hook_entity_field_storage_info().
49  */
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());
56     return $definitions;
57   }
58 }
59
60 /**
61  * Implements hook_entity_bundle_field_info().
62  */
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'];
66     return $definitions;
67   }
68 }
69
70 /**
71  * Implements hook_entity_bundle_create().
72  */
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']);
82   }
83 }
84
85 /**
86  * Implements hook_entity_bundle_delete().
87  */
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']);
98   }
99 }