workspaces.install 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @file
  4. * Contains install, update and uninstall functions for the Workspaces module.
  5. */
  6. use Drupal\Core\Entity\EntityTypeInterface;
  7. use Drupal\Core\Entity\ContentEntityNullStorage;
  8. use Drupal\Core\Field\BaseFieldDefinition;
  9. use Drupal\workspaces\Entity\Workspace;
  10. /**
  11. * Implements hook_requirements().
  12. */
  13. function workspaces_requirements($phase) {
  14. $requirements = [];
  15. if ($phase === 'install') {
  16. if (\Drupal::moduleHandler()->moduleExists('workspace')) {
  17. $requirements['workspace_incompatibility'] = [
  18. 'severity' => REQUIREMENT_ERROR,
  19. 'description' => t('Workspaces can not be installed when the contributed Workspace module is also installed. See the <a href=":link">upgrade path</a> page for more information on how to upgrade.', [
  20. ':link' => 'https://www.drupal.org/node/2987783',
  21. ]),
  22. ];
  23. }
  24. }
  25. return $requirements;
  26. }
  27. /**
  28. * Implements hook_module_preinstall().
  29. */
  30. function workspaces_module_preinstall($module) {
  31. if ($module !== 'workspaces') {
  32. return;
  33. }
  34. /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
  35. $workspace_manager = \Drupal::service('workspaces.manager');
  36. $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  37. foreach ($entity_definition_update_manager->getEntityTypes() as $entity_type) {
  38. $revision_metadata_keys = $entity_type->get('revision_metadata_keys');
  39. if ($workspace_manager->isEntityTypeSupported($entity_type)) {
  40. $entity_type->setRevisionMetadataKey('workspace', 'workspace');
  41. $entity_definition_update_manager->updateEntityType($entity_type);
  42. }
  43. }
  44. }
  45. /**
  46. * Implements hook_install().
  47. */
  48. function workspaces_install() {
  49. // Set the owner of these default workspaces to be first user which which has
  50. // the 'administrator' role. This way we avoid hard coding user ID 1 for sites
  51. // that prefer to not give it any special meaning.
  52. $admin_roles = \Drupal::entityTypeManager()->getStorage('user_role')->getQuery()
  53. ->condition('is_admin', TRUE)
  54. ->execute();
  55. if (!empty($admin_roles)) {
  56. $query = \Drupal::entityTypeManager()->getStorage('user')->getQuery()
  57. ->condition('roles', $admin_roles, 'IN')
  58. ->condition('status', 1)
  59. ->sort('uid', 'ASC')
  60. ->range(0, 1);
  61. $result = $query->execute();
  62. }
  63. // Default to user ID 1 if we could not find any other administrator users.
  64. $owner_id = !empty($result) ? reset($result) : 1;
  65. // Create a 'stage' workspace by default.
  66. Workspace::create([
  67. 'id' => 'stage',
  68. 'label' => 'Stage',
  69. 'uid' => $owner_id,
  70. ])->save();
  71. }
  72. /**
  73. * Implements hook_schema().
  74. */
  75. function workspaces_schema() {
  76. $schema['workspace_association'] = [
  77. 'description' => 'Stores the association between entity revisions and their workspace.',
  78. 'fields' => [
  79. 'workspace' => [
  80. 'type' => 'varchar_ascii',
  81. 'length' => 128,
  82. 'not null' => TRUE,
  83. 'default' => '',
  84. 'description' => 'The workspace ID.',
  85. ],
  86. 'target_entity_type_id' => [
  87. 'type' => 'varchar_ascii',
  88. 'length' => EntityTypeInterface::ID_MAX_LENGTH,
  89. 'not null' => TRUE,
  90. 'default' => '',
  91. 'description' => 'The ID of the associated entity type.',
  92. ],
  93. 'target_entity_id' => [
  94. 'type' => 'int',
  95. 'unsigned' => TRUE,
  96. 'not null' => TRUE,
  97. 'description' => 'The ID of the associated entity.',
  98. ],
  99. 'target_entity_revision_id' => [
  100. 'type' => 'int',
  101. 'unsigned' => TRUE,
  102. 'not null' => TRUE,
  103. 'description' => 'The revision ID of the associated entity.',
  104. ],
  105. ],
  106. 'indexes' => [
  107. 'target_entity_revision_id' => ['target_entity_revision_id'],
  108. ],
  109. 'primary key' => ['workspace', 'target_entity_type_id', 'target_entity_id'],
  110. ];
  111. return $schema;
  112. }
  113. /**
  114. * Add the 'workspace' revision metadata field on all supported entity types.
  115. */
  116. function workspaces_update_8801() {
  117. /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
  118. $workspace_manager = \Drupal::service('workspaces.manager');
  119. $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  120. foreach ($entity_definition_update_manager->getEntityTypes() as $entity_type_id => $entity_type) {
  121. if ($workspace_manager->isEntityTypeSupported($entity_type)) {
  122. $revision_metadata_keys = $entity_type->get('revision_metadata_keys');
  123. if (!isset($revision_metadata_keys['workspace'])) {
  124. $revision_metadata_field_name = 'workspace';
  125. // Bail out if there's an existing field called 'workspace'.
  126. if ($entity_definition_update_manager->getFieldStorageDefinition($revision_metadata_field_name, $entity_type_id)) {
  127. throw new \RuntimeException("An existing 'workspace' field was found for the '$entity_type_id' entity type. Set the 'workspace' revision metadata key to use a different field name and run this update function again.");
  128. }
  129. $entity_type->setRevisionMetadataKey('workspace', $revision_metadata_field_name);
  130. $entity_definition_update_manager->updateEntityType($entity_type);
  131. }
  132. else {
  133. $revision_metadata_field_name = $revision_metadata_keys['workspace'];
  134. }
  135. $field_storage = BaseFieldDefinition::create('entity_reference')
  136. ->setLabel(t('Workspace'))
  137. ->setDescription(t('Indicates the workspace that this revision belongs to.'))
  138. ->setSetting('target_type', 'workspace')
  139. ->setInternal(TRUE)
  140. ->setTranslatable(FALSE)
  141. ->setRevisionable(TRUE);
  142. $entity_definition_update_manager->installFieldStorageDefinition($revision_metadata_field_name, $entity_type_id, 'workspaces', $field_storage);
  143. }
  144. }
  145. return t("The 'workspace' revision metadata field has been installed.");
  146. }
  147. /**
  148. * Add the 'parent' field to the 'workspace' entity type.
  149. */
  150. function workspaces_update_8802() {
  151. $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  152. // Install the new 'parent' field.
  153. $storage_definition = BaseFieldDefinition::create('entity_reference')
  154. ->setLabel(t('Parent'))
  155. ->setDescription(t('The parent workspace.'))
  156. ->setSetting('target_type', 'workspace')
  157. ->setReadOnly(TRUE);
  158. $entity_definition_update_manager->installFieldStorageDefinition('parent', 'workspace', 'workspaces', $storage_definition);
  159. }
  160. /**
  161. * Remove the Workspace Association entity storage if necessary.
  162. */
  163. function workspaces_update_8803() {
  164. $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  165. $entity_type = $entity_definition_update_manager->getEntityType('workspace_association');
  166. // We can't migrate the workspace association data if the entity type is not
  167. // using its default storage.
  168. // @see workspaces_post_update_move_association_data()
  169. if ($entity_type && $entity_type->getHandlerClasses()['storage'] === 'Drupal\workspaces\WorkspaceAssociationStorage') {
  170. \Drupal::state()->set('workspaces_update_8803.tables', [
  171. 'base_table' => $entity_type->getBaseTable(),
  172. 'revision_table' => $entity_type->getRevisionTable(),
  173. ]);
  174. $entity_type->setStorageClass(ContentEntityNullStorage::class);
  175. $entity_definition_update_manager->uninstallEntityType($entity_type);
  176. }
  177. }