moduleExists('workspace')) { $requirements['workspace_incompatibility'] = [ 'severity' => REQUIREMENT_ERROR, 'description' => t('Workspaces can not be installed when the contributed Workspace module is also installed. See the upgrade path page for more information on how to upgrade.', [ ':link' => 'https://www.drupal.org/node/2987783', ]), ]; } } return $requirements; } /** * Implements hook_module_preinstall(). */ function workspaces_module_preinstall($module) { if ($module !== 'workspaces') { return; } /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */ $workspace_manager = \Drupal::service('workspaces.manager'); $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); foreach ($entity_definition_update_manager->getEntityTypes() as $entity_type) { $revision_metadata_keys = $entity_type->get('revision_metadata_keys'); if ($workspace_manager->isEntityTypeSupported($entity_type)) { $entity_type->setRevisionMetadataKey('workspace', 'workspace'); $entity_definition_update_manager->updateEntityType($entity_type); } } } /** * Implements hook_install(). */ function workspaces_install() { // Set the owner of these default workspaces to be first user which which has // the 'administrator' role. This way we avoid hard coding user ID 1 for sites // that prefer to not give it any special meaning. $admin_roles = \Drupal::entityTypeManager()->getStorage('user_role')->getQuery() ->condition('is_admin', TRUE) ->execute(); if (!empty($admin_roles)) { $query = \Drupal::entityTypeManager()->getStorage('user')->getQuery() ->condition('roles', $admin_roles, 'IN') ->condition('status', 1) ->sort('uid', 'ASC') ->range(0, 1); $result = $query->execute(); } // Default to user ID 1 if we could not find any other administrator users. $owner_id = !empty($result) ? reset($result) : 1; // Create a 'stage' workspace by default. Workspace::create([ 'id' => 'stage', 'label' => 'Stage', 'uid' => $owner_id, ])->save(); } /** * Implements hook_schema(). */ function workspaces_schema() { $schema['workspace_association'] = [ 'description' => 'Stores the association between entity revisions and their workspace.', 'fields' => [ 'workspace' => [ 'type' => 'varchar_ascii', 'length' => 128, 'not null' => TRUE, 'default' => '', 'description' => 'The workspace ID.', ], 'target_entity_type_id' => [ 'type' => 'varchar_ascii', 'length' => EntityTypeInterface::ID_MAX_LENGTH, 'not null' => TRUE, 'default' => '', 'description' => 'The ID of the associated entity type.', ], 'target_entity_id' => [ 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The ID of the associated entity.', ], 'target_entity_revision_id' => [ 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The revision ID of the associated entity.', ], ], 'indexes' => [ 'target_entity_revision_id' => ['target_entity_revision_id'], ], 'primary key' => ['workspace', 'target_entity_type_id', 'target_entity_id'], ]; return $schema; } /** * Add the 'workspace' revision metadata field on all supported entity types. */ function workspaces_update_8801() { /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */ $workspace_manager = \Drupal::service('workspaces.manager'); $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); foreach ($entity_definition_update_manager->getEntityTypes() as $entity_type_id => $entity_type) { if ($workspace_manager->isEntityTypeSupported($entity_type)) { $revision_metadata_keys = $entity_type->get('revision_metadata_keys'); if (!isset($revision_metadata_keys['workspace'])) { $revision_metadata_field_name = 'workspace'; // Bail out if there's an existing field called 'workspace'. if ($entity_definition_update_manager->getFieldStorageDefinition($revision_metadata_field_name, $entity_type_id)) { 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."); } $entity_type->setRevisionMetadataKey('workspace', $revision_metadata_field_name); $entity_definition_update_manager->updateEntityType($entity_type); } else { $revision_metadata_field_name = $revision_metadata_keys['workspace']; } $field_storage = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Workspace')) ->setDescription(t('Indicates the workspace that this revision belongs to.')) ->setSetting('target_type', 'workspace') ->setInternal(TRUE) ->setTranslatable(FALSE) ->setRevisionable(TRUE); $entity_definition_update_manager->installFieldStorageDefinition($revision_metadata_field_name, $entity_type_id, 'workspaces', $field_storage); } } return t("The 'workspace' revision metadata field has been installed."); } /** * Add the 'parent' field to the 'workspace' entity type. */ function workspaces_update_8802() { $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); // Install the new 'parent' field. $storage_definition = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Parent')) ->setDescription(t('The parent workspace.')) ->setSetting('target_type', 'workspace') ->setReadOnly(TRUE); $entity_definition_update_manager->installFieldStorageDefinition('parent', 'workspace', 'workspaces', $storage_definition); } /** * Remove the Workspace Association entity storage if necessary. */ function workspaces_update_8803() { $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); $entity_type = $entity_definition_update_manager->getEntityType('workspace_association'); // We can't migrate the workspace association data if the entity type is not // using its default storage. // @see workspaces_post_update_move_association_data() if ($entity_type && $entity_type->getHandlerClasses()['storage'] === 'Drupal\workspaces\WorkspaceAssociationStorage') { \Drupal::state()->set('workspaces_update_8803.tables', [ 'base_table' => $entity_type->getBaseTable(), 'revision_table' => $entity_type->getRevisionTable(), ]); $entity_type->setStorageClass(ContentEntityNullStorage::class); $entity_definition_update_manager->uninstallEntityType($entity_type); } }