You are here

workspaces.module in Drupal 9

Same filename and directory in other branches
  1. 8 core/modules/workspaces/workspaces.module

Provides full-site preview functionality for content staging.

File

core/modules/workspaces/workspaces.module
View source
<?php

/**
 * @file
 * Provides full-site preview functionality for content staging.
 */
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\workspaces\EntityAccess;
use Drupal\workspaces\EntityOperations;
use Drupal\workspaces\EntityTypeInfo;
use Drupal\workspaces\FormOperations;
use Drupal\workspaces\ViewsQueryAlter;

/**
 * Implements hook_help().
 */
function workspaces_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the Workspaces module.
    case 'help.page.workspaces':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Workspaces module allows workspaces to be defined and switched between. Content is then assigned to the active workspace when created. For more information, see the <a href=":workspaces">online documentation for the Workspaces module</a>.', [
        ':workspaces' => 'https://www.drupal.org/node/2824024',
      ]) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_entity_type_build().
 */
function workspaces_entity_type_build(array &$entity_types) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->entityTypeBuild($entity_types);
}

/**
 * Implements hook_entity_type_alter().
 */
function workspaces_entity_type_alter(array &$entity_types) {
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->entityTypeAlter($entity_types);
}

/**
 * Implements hook_form_alter().
 */
function workspaces_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_state
    ->getFormObject() instanceof EntityFormInterface) {
    \Drupal::service('class_resolver')
      ->getInstanceFromDefinition(EntityOperations::class)
      ->entityFormAlter($form, $form_state, $form_id);
  }
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(FormOperations::class)
    ->formAlter($form, $form_state, $form_id);
}

/**
 * Implements hook_field_info_alter().
 */
function workspaces_field_info_alter(&$definitions) {
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->fieldInfoAlter($definitions);
}

/**
 * Implements hook_entity_base_field_info().
 */
function workspaces_entity_base_field_info(EntityTypeInterface $entity_type) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->entityBaseFieldInfo($entity_type);
}

/**
 * Implements hook_entity_preload().
 */
function workspaces_entity_preload(array $ids, $entity_type_id) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityPreload($ids, $entity_type_id);
}

/**
 * Implements hook_entity_presave().
 */
function workspaces_entity_presave(EntityInterface $entity) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityPresave($entity);
}

/**
 * Implements hook_entity_insert().
 */
function workspaces_entity_insert(EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() === 'workspace') {
    \Drupal::service('workspaces.association')
      ->workspaceInsert($entity);
    \Drupal::service('workspaces.repository')
      ->resetCache();
  }
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityInsert($entity);
}

/**
 * Implements hook_entity_update().
 */
function workspaces_entity_update(EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() === 'workspace') {
    \Drupal::service('workspaces.repository')
      ->resetCache();
  }
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityUpdate($entity);
}

/**
 * Implements hook_entity_predelete().
 */
function workspaces_entity_predelete(EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() === 'workspace') {
    \Drupal::service('workspaces.repository')
      ->resetCache();
  }
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityPredelete($entity);
}

/**
 * Implements hook_entity_access().
 *
 * @see \Drupal\workspaces\EntityAccess
 */
function workspaces_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityAccess::class)
    ->entityOperationAccess($entity, $operation, $account);
}

/**
 * Implements hook_entity_create_access().
 *
 * @see \Drupal\workspaces\EntityAccess
 */
function workspaces_entity_create_access(AccountInterface $account, array $context, $entity_bundle) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityAccess::class)
    ->entityCreateAccess($account, $context, $entity_bundle);
}

/**
 * Implements hook_views_query_alter().
 */
function workspaces_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(ViewsQueryAlter::class)
    ->alterQuery($view, $query);
}

/**
 * Implements hook_cron().
 */
function workspaces_cron() {
  \Drupal::service('workspaces.manager')
    ->purgeDeletedWorkspacesBatch();
}

/**
 * Implements hook_toolbar().
 */
function workspaces_toolbar() {
  $items['workspace'] = [
    '#cache' => [
      'contexts' => [
        'user.permissions',
      ],
    ],
  ];
  $current_user = \Drupal::currentUser();
  if (!$current_user
    ->hasPermission('administer workspaces') && !$current_user
    ->hasPermission('view own workspace') && !$current_user
    ->hasPermission('view any workspace')) {
    return $items;
  }

  /** @var \Drupal\workspaces\WorkspaceInterface $active_workspace */
  $active_workspace = \Drupal::service('workspaces.manager')
    ->getActiveWorkspace();
  $items['workspace'] += [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => $active_workspace ? $active_workspace
        ->label() : t('Live'),
      '#url' => Url::fromRoute('entity.workspace.collection', [], [
        'query' => \Drupal::destination()
          ->getAsArray(),
      ]),
      '#attributes' => [
        'title' => t('Switch workspace'),
        'class' => [
          'use-ajax',
          'toolbar-icon',
          'toolbar-icon-workspace',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas_top',
        'data-dialog-options' => Json::encode([
          'height' => 161,
          'classes' => [
            'ui-dialog' => 'workspaces-dialog',
          ],
        ]),
      ],
      '#cache' => [
        'tags' => $active_workspace ? $active_workspace
          ->getCacheTags() : [],
      ],
    ],
    '#wrapper_attributes' => [
      'class' => [
        'workspaces-toolbar-tab',
      ],
    ],
    '#attached' => [
      'library' => [
        'workspaces/drupal.workspaces.toolbar',
      ],
    ],
    '#weight' => 500,
  ];

  // Add a special class to the wrapper if we don't have an active workspace so
  // we can highlight it with a different color.
  if (!$active_workspace) {
    $items['workspace']['#wrapper_attributes']['class'][] = 'workspaces-toolbar-tab--is-default';
  }
  return $items;
}