123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?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;
- }
|