5b32eb8863f2cc758af448f370dbb6b1411c05de
[yaffs-website] / web / core / modules / system / tests / modules / entity_test_update / entity_test_update.module
1 <?php
2
3 /**
4  * @file
5  * Provides an entity type for testing definition and schema updates.
6  */
7
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\Core\StringTranslation\TranslatableMarkup;
11 use Drupal\entity_test_update\Entity\EntityTestUpdate;
12 use Drupal\Core\Entity\EntityTypeInterface;
13
14 /**
15  * Implements hook_entity_base_field_info().
16  */
17 function entity_test_update_entity_base_field_info(EntityTypeInterface $entity_type) {
18   // Add a base field that will be used to test that fields added through
19   // hook_entity_base_field_info() are handled correctly during a schema
20   // conversion (e.g. from non-revisionable to revisionable).
21   if ($entity_type->id() == 'entity_test_update') {
22     $fields = [];
23     $fields['test_entity_base_field_info'] = BaseFieldDefinition::create('string')
24       ->setLabel(new TranslatableMarkup('Field added by hook_entity_base_field_info()'))
25       ->setTranslatable(TRUE)
26       ->setRevisionable(TRUE);
27
28     return $fields;
29   }
30 }
31
32 /**
33  * Implements hook_entity_field_storage_info().
34  */
35 function entity_test_update_entity_field_storage_info(EntityTypeInterface $entity_type) {
36   if ($entity_type->id() == 'entity_test_update') {
37     return \Drupal::state()->get('entity_test_update.additional_field_storage_definitions', []);
38   }
39 }
40
41 /**
42  * Implements hook_entity_type_alter().
43  */
44 function entity_test_update_entity_type_alter(array &$entity_types) {
45   // Allow entity_test_update tests to override the entity type definition.
46   $entity_types['entity_test_update'] = \Drupal::state()->get('entity_test_update.entity_type', $entity_types['entity_test_update']);
47 }
48
49 /**
50  * Implements hook_entity_presave().
51  */
52 function entity_test_update_entity_presave(EntityInterface $entity) {
53   // Simulate an error during the save process of a test entity.
54   if ($entity->getEntityTypeId() === 'entity_test_update' && \Drupal::state()->get('entity_test_update.throw_exception', FALSE)) {
55     throw new \Exception('Peekaboo!');
56   }
57 }
58
59 /**
60  * Creates a given number of 'entity_test_update' entities.
61  *
62  * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz' db dump was
63  * created with the following characteristics:
64  * - Drupal 8.0.0-rc1 installed with the Standard profile;
65  * - The 'entity_test_update' module enabled;
66  * - 102 test entities created and saved.
67  *
68  * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz' db dump
69  * was created with the following characteristics:
70  * - Drupal 8.0.0-rc1 installed with the Standard profile;
71  * - Manually edited the annotation of the entity_test_update entity type in
72  *   order to make it translatable;
73  * - The entity_test_update and Language module enabled;
74  * - Romanian language added;
75  * - 50 test entities created, translated and saved;
76  * - The Content Translation module enabled and configured for our test entity
77  *   type. This was enabled after the first 50 entities were created in order
78  *   to have NULL values for its translation metadata fields
79  *   (e.g. content_translation_status);
80  * - 52 more test entities (with the IDs 51 - 102) crated, translated and saved.
81  *
82  * @param int $start
83  *   (optional) The entity ID to start from. Defaults to 1.
84  * @param int $end
85  *   (optional) The entity ID to end with. Defaults to 50.
86  * @param bool $add_translation
87  *   (optional) Whether to create a translation for each entity. If the number
88  *   of entities is greater than 50, the default translation (en) of the 51st
89  *   entity and the 52nd translation (ro) are disabled through the
90  *   'content_moderation_status' field. Defaults to FALSE.
91  *
92  * @see drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz
93  * @see drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz
94  */
95 function _entity_test_update_create_test_entities($start = 1, $end = 50, $add_translation = FALSE) {
96   for ($i = $start; $i <= $end; $i++) {
97     $entity  = EntityTestUpdate::create([
98       'id' => $i,
99       'name' => $i,
100       'test_single_property' => $i . ' - test single property',
101       'test_multiple_properties' => [[
102         'value1' => $i . ' - test multiple properties - value1',
103         'value2' => $i . ' - test multiple properties - value2',
104       ]],
105       'test_single_property_multiple_values' => [
106         ['value' => $i . ' - test single property multiple values 0'],
107         ['value' => $i . ' - test single property multiple values 1']
108       ],
109       'test_multiple_properties_multiple_values' => [
110         [
111           'value1' => $i . ' - test multiple properties multiple values - value1 0',
112           'value2' => $i . ' - test multiple properties multiple values - value2 0',
113         ],
114         [
115           'value1' => $i . ' - test multiple properties multiple values - value1 1',
116           'value2' => $i . ' - test multiple properties multiple values - value2 1',
117         ]
118       ],
119       'test_non_rev_field' => $i . ' - test non-revisionable field',
120       'test_non_mul_field' => $i . ' - test non-translatable field',
121       'test_non_mulrev_field' => $i . ' - test non-translatable and non-revisionable field',
122       'field_test_configurable_field' => [
123         [
124           'value1' => $i . ' - field test configurable field - value1 0',
125           'value2' => $i . ' - field test configurable field - value2 0',
126         ],
127         [
128           'value1' => $i . ' - field test configurable field - value1 1',
129           'value2' => $i . ' - field test configurable field - value2 1',
130         ]
131       ],
132       'test_entity_base_field_info' => $i . ' - test entity base field info',
133     ]);
134
135     if ($add_translation) {
136       $entity->addTranslation('ro', [
137         'name' => $i . ' - ro',
138         'test_single_property' => $i . ' - test single property - ro',
139         'test_multiple_properties' => [[
140           'value1' => $i . ' - test multiple properties - value1 - ro',
141           'value2' => $i . ' - test multiple properties - value2 - ro',
142         ]],
143         'test_single_property_multiple_values' => [
144           ['value' => $i . ' - test single property multiple values 0 - ro'],
145           ['value' => $i . ' - test single property multiple values 1 - ro']
146         ],
147         'test_multiple_properties_multiple_values' => [
148           [
149             'value1' => $i . ' - test multiple properties multiple values - value1 0 - ro',
150             'value2' => $i . ' - test multiple properties multiple values - value2 0 - ro',
151           ],
152           [
153             'value1' => $i . ' - test multiple properties multiple values - value1 1 - ro',
154             'value2' => $i . ' - test multiple properties multiple values - value2 1 - ro',
155           ]
156         ],
157         'test_non_rev_field' => $i . ' - test non-revisionable field - ro',
158         'field_test_configurable_field' => [
159           [
160             'value1' => $i . ' - field test configurable field - value1 0 - ro',
161             'value2' => $i . ' - field test configurable field - value2 0 - ro',
162           ],
163           [
164             'value1' => $i . ' - field test configurable field - value1 1 - ro',
165             'value2' => $i . ' - field test configurable field - value2 1 - ro',
166           ]
167         ],
168         'test_entity_base_field_info' => $i . ' - test entity base field info - ro',
169       ]);
170
171       if ($i == 101) {
172         $entity->getTranslation('en')->set('content_translation_status', FALSE);
173       }
174       if ($i == 102) {
175         $entity->getTranslation('ro')->set('content_translation_status', FALSE);
176       }
177     }
178
179     $entity->save();
180   }
181 }