5 * Provides an entity type for testing definition and schema updates.
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;
15 * Implements hook_entity_base_field_info().
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') {
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);
33 * Implements hook_entity_field_storage_info().
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', []);
42 * Implements hook_entity_type_alter().
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']);
50 * Implements hook_entity_presave().
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!');
60 * Creates a given number of 'entity_test_update' entities.
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.
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.
83 * (optional) The entity ID to start from. Defaults to 1.
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.
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
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([
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',
105 'test_single_property_multiple_values' => [
106 ['value' => $i . ' - test single property multiple values 0'],
107 ['value' => $i . ' - test single property multiple values 1']
109 'test_multiple_properties_multiple_values' => [
111 'value1' => $i . ' - test multiple properties multiple values - value1 0',
112 'value2' => $i . ' - test multiple properties multiple values - value2 0',
115 'value1' => $i . ' - test multiple properties multiple values - value1 1',
116 'value2' => $i . ' - test multiple properties multiple values - value2 1',
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' => [
124 'value1' => $i . ' - field test configurable field - value1 0',
125 'value2' => $i . ' - field test configurable field - value2 0',
128 'value1' => $i . ' - field test configurable field - value1 1',
129 'value2' => $i . ' - field test configurable field - value2 1',
132 'test_entity_base_field_info' => $i . ' - test entity base field info',
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',
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']
147 'test_multiple_properties_multiple_values' => [
149 'value1' => $i . ' - test multiple properties multiple values - value1 0 - ro',
150 'value2' => $i . ' - test multiple properties multiple values - value2 0 - ro',
153 'value1' => $i . ' - test multiple properties multiple values - value1 1 - ro',
154 'value2' => $i . ' - test multiple properties multiple values - value2 1 - ro',
157 'test_non_rev_field' => $i . ' - test non-revisionable field - ro',
158 'field_test_configurable_field' => [
160 'value1' => $i . ' - field test configurable field - value1 0 - ro',
161 'value2' => $i . ' - field test configurable field - value2 0 - ro',
164 'value1' => $i . ' - field test configurable field - value1 1 - ro',
165 'value2' => $i . ' - field test configurable field - value2 1 - ro',
168 'test_entity_base_field_info' => $i . ' - test entity base field info - ro',
172 $entity->getTranslation('en')->set('content_translation_status', FALSE);
175 $entity->getTranslation('ro')->set('content_translation_status', FALSE);