You are here

function workspaces_update_8801 in Drupal 8

Add the 'workspace' revision metadata field on all supported entity types.

File

core/modules/workspaces/workspaces.install, line 128
Contains install, update and uninstall functions for the Workspaces module.

Code

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.");
}