86ca5a9712c329fabc66d85f7b8528b42a044252
[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_type_alter().
34  */
35 function entity_test_update_entity_type_alter(array &$entity_types) {
36   // Allow entity_test_update tests to override the entity type definition.
37   $entity_types['entity_test_update'] = \Drupal::state()->get('entity_test_update.entity_type', $entity_types['entity_test_update']);
38 }
39
40 /**
41  * Implements hook_entity_presave().
42  */
43 function entity_test_update_entity_presave(EntityInterface $entity) {
44   // Simulate an error during the save process of a test entity.
45   if ($entity->getEntityTypeId() === 'entity_test_update' && \Drupal::state()->get('entity_test_update.throw_exception', FALSE)) {
46     throw new \Exception('Peekaboo!');
47   }
48 }
49
50 /**
51  * Creates a given number of 'entity_test_update' entities.
52  *
53  * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz' db dump was
54  * created with the following characteristics:
55  * - Drupal 8.0.0-rc1 installed with the Standard profile;
56  * - The 'entity_test_update' module enabled;
57  * - 102 test entities created and saved.
58  *
59  * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz' db dump
60  * was created with the following characteristics:
61  * - Drupal 8.0.0-rc1 installed with the Standard profile;
62  * - Manually edited the annotation of the entity_test_update entity type in
63  *   order to make it translatable;
64  * - The entity_test_update and Language module enabled;
65  * - Romanian language added;
66  * - 50 test entities created, translated and saved;
67  * - The Content Translation module enabled and configured for our test entity
68  *   type. This was enabled after the first 50 entities were created in order
69  *   to have NULL values for its translation metadata fields
70  *   (e.g. content_translation_status);
71  * - 52 more test entities (with the IDs 51 - 102) crated, translated and saved.
72  *
73  * @param int $start
74  *   (optional) The entity ID to start from. Defaults to 1.
75  * @param int $end
76  *   (optional) The entity ID to end with. Defaults to 50.
77  * @param bool $add_translation
78  *   (optional) Whether to create a translation for each entity. If the number
79  *   of entities is greater than 50, the default translation (en) of the 51st
80  *   entity and the 52nd translation (ro) are disabled through the
81  *   'content_moderation_status' field. Defaults to FALSE.
82  *
83  * @see drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz
84  * @see drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz
85  */
86 function _entity_test_update_create_test_entities($start = 1, $end = 50, $add_translation = FALSE) {
87   for ($i = $start; $i <= $end; $i++) {
88     $entity  = EntityTestUpdate::create([
89       'id' => $i,
90       'name' => $i,
91       'test_single_property' => $i . ' - test single property',
92       'test_multiple_properties' => [[
93         'value1' => $i . ' - test multiple properties - value1',
94         'value2' => $i . ' - test multiple properties - value2',
95       ]],
96       'test_single_property_multiple_values' => [
97         ['value' => $i . ' - test single property multiple values 0'],
98         ['value' => $i . ' - test single property multiple values 1']
99       ],
100       'test_multiple_properties_multiple_values' => [
101         [
102           'value1' => $i . ' - test multiple properties multiple values - value1 0',
103           'value2' => $i . ' - test multiple properties multiple values - value2 0',
104         ],
105         [
106           'value1' => $i . ' - test multiple properties multiple values - value1 1',
107           'value2' => $i . ' - test multiple properties multiple values - value2 1',
108         ]
109       ],
110       'test_non_rev_field' => $i . ' - test non-revisionable field',
111       'test_non_mul_field' => $i . ' - test non-translatable field',
112       'test_non_mulrev_field' => $i . ' - test non-translatable and non-revisionable field',
113       'field_test_configurable_field' => [
114         [
115           'value1' => $i . ' - field test configurable field - value1 0',
116           'value2' => $i . ' - field test configurable field - value2 0',
117         ],
118         [
119           'value1' => $i . ' - field test configurable field - value1 1',
120           'value2' => $i . ' - field test configurable field - value2 1',
121         ]
122       ],
123       'test_entity_base_field_info' => $i . ' - test entity base field info',
124     ]);
125
126     if ($add_translation) {
127       $entity->addTranslation('ro', [
128         'name' => $i . ' - ro',
129         'test_single_property' => $i . ' - test single property - ro',
130         'test_multiple_properties' => [[
131           'value1' => $i . ' - test multiple properties - value1 - ro',
132           'value2' => $i . ' - test multiple properties - value2 - ro',
133         ]],
134         'test_single_property_multiple_values' => [
135           ['value' => $i . ' - test single property multiple values 0 - ro'],
136           ['value' => $i . ' - test single property multiple values 1 - ro']
137         ],
138         'test_multiple_properties_multiple_values' => [
139           [
140             'value1' => $i . ' - test multiple properties multiple values - value1 0 - ro',
141             'value2' => $i . ' - test multiple properties multiple values - value2 0 - ro',
142           ],
143           [
144             'value1' => $i . ' - test multiple properties multiple values - value1 1 - ro',
145             'value2' => $i . ' - test multiple properties multiple values - value2 1 - ro',
146           ]
147         ],
148         'test_non_rev_field' => $i . ' - test non-revisionable field - ro',
149         'field_test_configurable_field' => [
150           [
151             'value1' => $i . ' - field test configurable field - value1 0 - ro',
152             'value2' => $i . ' - field test configurable field - value2 0 - ro',
153           ],
154           [
155             'value1' => $i . ' - field test configurable field - value1 1 - ro',
156             'value2' => $i . ' - field test configurable field - value2 1 - ro',
157           ]
158         ],
159         'test_entity_base_field_info' => $i . ' - test entity base field info - ro',
160       ]);
161
162       if ($i == 101) {
163         $entity->getTranslation('en')->set('content_translation_status', FALSE);
164       }
165       if ($i == 102) {
166         $entity->getTranslation('ro')->set('content_translation_status', FALSE);
167       }
168     }
169
170     $entity->save();
171   }
172 }