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_type_alter().
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']);
41 * Implements hook_entity_presave().
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!');
51 * Creates a given number of 'entity_test_update' entities.
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.
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.
74 * (optional) The entity ID to start from. Defaults to 1.
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.
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
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([
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',
96 'test_single_property_multiple_values' => [
97 ['value' => $i . ' - test single property multiple values 0'],
98 ['value' => $i . ' - test single property multiple values 1']
100 'test_multiple_properties_multiple_values' => [
102 'value1' => $i . ' - test multiple properties multiple values - value1 0',
103 'value2' => $i . ' - test multiple properties multiple values - value2 0',
106 'value1' => $i . ' - test multiple properties multiple values - value1 1',
107 'value2' => $i . ' - test multiple properties multiple values - value2 1',
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' => [
115 'value1' => $i . ' - field test configurable field - value1 0',
116 'value2' => $i . ' - field test configurable field - value2 0',
119 'value1' => $i . ' - field test configurable field - value1 1',
120 'value2' => $i . ' - field test configurable field - value2 1',
123 'test_entity_base_field_info' => $i . ' - test entity base field info',
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',
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']
138 'test_multiple_properties_multiple_values' => [
140 'value1' => $i . ' - test multiple properties multiple values - value1 0 - ro',
141 'value2' => $i . ' - test multiple properties multiple values - value2 0 - ro',
144 'value1' => $i . ' - test multiple properties multiple values - value1 1 - ro',
145 'value2' => $i . ' - test multiple properties multiple values - value2 1 - ro',
148 'test_non_rev_field' => $i . ' - test non-revisionable field - ro',
149 'field_test_configurable_field' => [
151 'value1' => $i . ' - field test configurable field - value1 0 - ro',
152 'value2' => $i . ' - field test configurable field - value2 0 - ro',
155 'value1' => $i . ' - field test configurable field - value1 1 - ro',
156 'value2' => $i . ' - field test configurable field - value2 1 - ro',
159 'test_entity_base_field_info' => $i . ' - test entity base field info - ro',
163 $entity->getTranslation('en')->set('content_translation_status', FALSE);
166 $entity->getTranslation('ro')->set('content_translation_status', FALSE);