Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / modules / entity_test_update / update / entity_rev_pub_updates_8400.inc
1 <?php
2
3 /**
4  * @file
5  * Defines the 8400 db update for the "entity_rev_pub_updates" group.
6  */
7
8 use Drupal\Core\Field\BaseFieldDefinition;
9
10 /**
11  * Add the 'published' entity key to entity_test_update.
12  */
13 function entity_test_update_update_8400() {
14   $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
15
16   // Add the published entity key to the entity_test_update entity type.
17   $entity_type = $definition_update_manager->getEntityType('entity_test_update');
18
19   $entity_keys = $entity_type->getKeys();
20   $entity_keys['published'] = 'status';
21   $entity_type->set('entity_keys', $entity_keys);
22   $definition_update_manager->updateEntityType($entity_type);
23
24   // Add the status field.
25   $status = BaseFieldDefinition::create('boolean')
26     ->setLabel(t('Publishing status'))
27     ->setDescription(t('A boolean indicating the published state.'))
28     ->setRevisionable(TRUE)
29     ->setTranslatable(TRUE)
30     ->setDefaultValue(TRUE);
31
32   $has_content_translation_status_field = \Drupal::moduleHandler()->moduleExists('content_translation') && $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'entity_test_update');
33   if ($has_content_translation_status_field) {
34     $status->setInitialValueFromField('content_translation_status', TRUE);
35   }
36   else {
37     $status->setInitialValue(TRUE);
38   }
39   $definition_update_manager->installFieldStorageDefinition('status', 'entity_test_update', 'entity_test_update', $status);
40
41   // Uninstall the 'content_translation_status' field if needed.
42   $database = \Drupal::database();
43   if ($has_content_translation_status_field) {
44     // First we have to remove the field data.
45     $database->update($entity_type->getDataTable())
46       ->fields(['content_translation_status' => NULL])
47       ->execute();
48
49     // A site may have disabled revisionability for this entity type.
50     if ($entity_type->isRevisionable()) {
51       $database->update($entity_type->getRevisionDataTable())
52         ->fields(['content_translation_status' => NULL])
53         ->execute();
54     }
55
56     $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'entity_test_update');
57     $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
58   }
59 }