view core/modules/workspaces/workspaces.module @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
line wrap: on
line 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\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
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_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_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) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityInsert($entity);
}

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

/**
 * Implements hook_entity_predelete().
 */
function workspaces_entity_predelete(EntityInterface $entity) {
  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 = [];
  $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->label(),
      '#url' => $active_workspace->toUrl('collection'),
      '#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->getCacheTags()],
    ],
    '#wrapper_attributes' => [
      'class' => ['workspaces-toolbar-tab'],
    ],
    '#attached' => [
      'library' => ['workspaces/drupal.workspaces.toolbar'],
    ],
    '#weight' => 500,
  ];

  // Add a special class to the wrapper if we are in the default workspace so we
  // can highlight it with a different color.
  if ($active_workspace->isDefaultWorkspace()) {
    $items['workspace']['#wrapper_attributes']['class'][] = 'workspaces-toolbar-tab--is-default';
  }

  return $items;
}