workspaces.module

Same filename in other branches
  1. 9 core/modules/workspaces/workspaces.module
  2. 8.9.x core/modules/workspaces/workspaces.module
  3. 11.x 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\Core\Cache\Cache;
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 .= '<h2>' . t('About') . '</h2>';
            $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/docs/8/core/modules/workspace/overview',
            ]) . '</p>';
            return $output;
    }
}

/**
 * Implements hook_module_implements_alter().
 */
function workspaces_module_implements_alter(&$implementations, $hook) : void {
    // Move our 'hook_entity_presave' implementation at the beginning to ensure
    // that other presave implementations are aware of the changes done in
    // \Drupal\workspaces\EntityOperations::entityPresave().
    if ($hook === 'entity_presave') {
        $implementation = $implementations['workspaces'];
        $implementations = [
            'workspaces' => $implementation,
        ] + $implementations;
    }
    // Move our 'hook_entity_insert' implementation at the end to ensure that
    // the second (pending) revision created for published entities is not used
    // by other 'hook_entity_insert' implementations.
    // @see \Drupal\workspaces\EntityOperations::entityInsert()
    if ($hook === 'entity_insert') {
        $group = $implementations['workspaces'];
        unset($implementations['workspaces']);
        $implementations['workspaces'] = $group;
    }
}

/**
 * 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_translation_insert().
 */
function workspaces_entity_translation_insert(EntityInterface $translation) : void {
    \Drupal::service('class_resolver')->getInstanceFromDefinition(EntityOperations::class)
        ->entityTranslationInsert($translation);
}

/**
 * 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_delete().
 */
function workspaces_entity_delete(EntityInterface $entity) {
    if (\Drupal::service('workspaces.information')->isEntityTypeSupported($entity->getEntityType())) {
        \Drupal::service('workspaces.association')->deleteAssociations(NULL, $entity->getEntityTypeId(), [
            $entity->id(),
        ]);
    }
}

/**
 * Implements hook_entity_revision_delete().
 */
function workspaces_entity_revision_delete(EntityInterface $entity) {
    if (\Drupal::service('workspaces.information')->isEntityTypeSupported($entity->getEntityType())) {
        \Drupal::service('workspaces.association')->deleteAssociations(NULL, $entity->getEntityTypeId(), [
            $entity->id(),
        ], [
            $entity->getRevisionId(),
        ]);
    }
}

/**
 * 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_ENTITY_TYPE_update() for the 'menu_link_content' entity type.
 */
function workspaces_menu_link_content_update(EntityInterface $entity) {
    
    /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
    if ($entity->getLoadedRevisionId() != $entity->getRevisionId()) {
        // We are not updating the menu tree definitions when a custom menu link
        // entity is saved as a pending revision (because the parent can not be
        // changed), so we need to clear the system menu cache manually. However,
        // inserting or deleting a custom menu link updates the menu tree
        // definitions, so we don't have to do anything in those cases.
        $cache_tags = Cache::buildTags('config:system.menu', [
            $entity->getMenuName(),
        ], '.');
        \Drupal::service('cache_tags.invalidator')->invalidateTags($cache_tags);
    }
}

/**
 * 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' => [
            '#lazy_builder' => [
                'workspaces.lazy_builders:renderToolbarTab',
                [],
            ],
            '#create_placeholder' => TRUE,
            '#lazy_builder_preview' => [
                '#type' => 'link',
                '#title' => $active_workspace ? $active_workspace->label() : t('Live'),
                '#url' => Url::fromRoute('entity.workspace.collection'),
                '#attributes' => [
                    'class' => [
                        'toolbar-tray-lazy-placeholder-link',
                    ],
                ],
            ],
        ],
        '#wrapper_attributes' => [
            'class' => [
                'workspaces-toolbar-tab',
            ],
        ],
        '#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';
    }
    // \Drupal\toolbar\Element\ToolbarItem::preRenderToolbarItem adds an
    // #attributes property to each toolbar item's tab child automatically.
    // Lazy builders don't support an #attributes property so we need to
    // add another render callback to remove the #attributes property. We start by
    // adding the defaults, and then we append our own pre render callback.
    $items['workspace'] += \Drupal::service('plugin.manager.element_info')->getInfo('toolbar_item');
    $items['workspace']['#pre_render'][] = 'workspaces.lazy_builders:removeTabAttributes';
    return $items;
}

Functions

Title Deprecated Summary
workspaces_cron Implements hook_cron().
workspaces_entity_access Implements hook_entity_access().
workspaces_entity_base_field_info Implements hook_entity_base_field_info().
workspaces_entity_create_access Implements hook_entity_create_access().
workspaces_entity_delete Implements hook_entity_delete().
workspaces_entity_insert Implements hook_entity_insert().
workspaces_entity_predelete Implements hook_entity_predelete().
workspaces_entity_preload Implements hook_entity_preload().
workspaces_entity_presave Implements hook_entity_presave().
workspaces_entity_revision_delete Implements hook_entity_revision_delete().
workspaces_entity_translation_insert Implements hook_entity_translation_insert().
workspaces_entity_type_alter Implements hook_entity_type_alter().
workspaces_entity_type_build Implements hook_entity_type_build().
workspaces_entity_update Implements hook_entity_update().
workspaces_field_info_alter Implements hook_field_info_alter().
workspaces_form_alter Implements hook_form_alter().
workspaces_help Implements hook_help().
workspaces_menu_link_content_update Implements hook_ENTITY_TYPE_update() for the 'menu_link_content' entity type.
workspaces_module_implements_alter Implements hook_module_implements_alter().
workspaces_toolbar Implements hook_toolbar().
workspaces_views_query_alter Implements hook_views_query_alter().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.