c6556e719c2753669cc8f52089b45e686433f047
[yaffs-website] / web / core / modules / block_content / block_content.install
1 <?php
2
3 /**
4  * @file
5  * Install, update and uninstall functions for the block_content module.
6  */
7
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\Core\StringTranslation\TranslatableMarkup;
10
11 /**
12  * Implements hook_update_dependencies().
13  */
14 function block_content_update_dependencies() {
15   // The update function that adds the status field must run after
16   // content_translation_update_8400() which fixes NULL values for the
17   // 'content_translation_status' field.
18   if (\Drupal::moduleHandler()->moduleExists('content_translation')) {
19     $dependencies['block_content'][8400] = [
20       'content_translation' => 8400,
21     ];
22
23     return $dependencies;
24   }
25 }
26
27 /**
28  * Add 'revision_translation_affected' field to 'block_content' entities.
29  */
30 function block_content_update_8001() {
31   // Install the definition that this field had in
32   // \Drupal\block_content\Entity\BlockContent::baseFieldDefinitions()
33   // at the time that this update function was written. If/when code is
34   // deployed that changes that definition, the corresponding module must
35   // implement an update function that invokes
36   // \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition()
37   // with the new definition.
38   $storage_definition = BaseFieldDefinition::create('boolean')
39     ->setLabel(t('Revision translation affected'))
40     ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
41     ->setReadOnly(TRUE)
42     ->setRevisionable(TRUE)
43     ->setTranslatable(TRUE);
44
45   \Drupal::entityDefinitionUpdateManager()
46     ->installFieldStorageDefinition('revision_translation_affected', 'block_content', 'block_content', $storage_definition);
47 }
48
49 /**
50  * Generalizes the d6_block_content_type and d6_block_content_body_field
51  * migrations.
52  */
53 function block_content_update_8002() {
54   // Removed in issue #2569605. The Migrate and Migrate Drupal modules are
55   // marked experimental and do not need to support the update path until they
56   // are stable.
57   // @see https://www.drupal.org/node/2569469
58 }
59
60 /**
61  * Add 'revision_created' and 'revision_user' fields to 'block_content' entities.
62  */
63 function block_content_update_8003() {
64   $revision_created = BaseFieldDefinition::create('created')
65     ->setLabel(t('Revision create time'))
66     ->setDescription(t('The time that the current revision was created.'))
67     ->setRevisionable(TRUE);
68
69   \Drupal::entityDefinitionUpdateManager()
70     ->installFieldStorageDefinition('revision_created', 'block_content', 'block_content', $revision_created);
71
72   $revision_user = BaseFieldDefinition::create('entity_reference')
73     ->setLabel(t('Revision user'))
74     ->setDescription(t('The user ID of the author of the current revision.'))
75     ->setSetting('target_type', 'user')
76     ->setRevisionable(TRUE);
77
78   \Drupal::entityDefinitionUpdateManager()
79     ->installFieldStorageDefinition('revision_user', 'block_content', 'block_content', $revision_user);
80 }
81
82 /**
83  * Fix the block_content entity type to specify its revision data table.
84  */
85 function block_content_update_8300() {
86   $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
87   $entity_type = $definition_update_manager->getEntityType('block_content');
88   $entity_type->set('revision_data_table', 'block_content_field_revision');
89   $definition_update_manager->updateEntityType($entity_type);
90 }
91
92 /**
93  * Add a publishing status field for block_content entities.
94  */
95 function block_content_update_8400() {
96   $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
97
98   // Add the published entity key to the block_content entity type.
99   $entity_type = $definition_update_manager->getEntityType('block_content');
100   $entity_keys = $entity_type->getKeys();
101   $entity_keys['published'] = 'status';
102   $entity_type->set('entity_keys', $entity_keys);
103   $definition_update_manager->updateEntityType($entity_type);
104
105   // Add the publishing status field to the block_content entity type.
106   $status = BaseFieldDefinition::create('boolean')
107     ->setLabel(new TranslatableMarkup('Publishing status'))
108     ->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
109     ->setRevisionable(TRUE)
110     ->setTranslatable(TRUE)
111     ->setDefaultValue(TRUE);
112
113   $has_content_translation_status_field = \Drupal::moduleHandler()->moduleExists('content_translation') && $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'block_content');
114   if ($has_content_translation_status_field) {
115     $status->setInitialValueFromField('content_translation_status');
116   }
117   else {
118     $status->setInitialValue(TRUE);
119   }
120   $definition_update_manager->installFieldStorageDefinition('status', 'block_content', 'block_content', $status);
121
122   // Uninstall the 'content_translation_status' field if needed.
123   $database = \Drupal::database();
124   if ($has_content_translation_status_field) {
125     // First we have to remove the field data.
126     $database->update($entity_type->getDataTable())
127       ->fields(['content_translation_status' => NULL])
128       ->execute();
129
130     // A site may have disabled revisionability for this entity type.
131     if ($entity_type->isRevisionable()) {
132       $database->update($entity_type->getRevisionDataTable())
133         ->fields(['content_translation_status' => NULL])
134         ->execute();
135     }
136
137     $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'block_content');
138     $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
139   }
140 }