annotate core/modules/workspaces/workspaces.module @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 <?php
Chris@4 2
Chris@4 3 /**
Chris@4 4 * @file
Chris@4 5 * Provides full-site preview functionality for content staging.
Chris@4 6 */
Chris@4 7
Chris@4 8 use Drupal\Component\Serialization\Json;
Chris@4 9 use Drupal\Core\Entity\EntityFormInterface;
Chris@4 10 use Drupal\Core\Entity\EntityInterface;
Chris@4 11 use Drupal\Core\Form\FormStateInterface;
Chris@4 12 use Drupal\Core\Routing\RouteMatchInterface;
Chris@4 13 use Drupal\Core\Session\AccountInterface;
Chris@4 14 use Drupal\views\Plugin\views\query\QueryPluginBase;
Chris@4 15 use Drupal\views\ViewExecutable;
Chris@4 16 use Drupal\workspaces\EntityAccess;
Chris@4 17 use Drupal\workspaces\EntityOperations;
Chris@4 18 use Drupal\workspaces\EntityTypeInfo;
Chris@4 19 use Drupal\workspaces\FormOperations;
Chris@4 20 use Drupal\workspaces\ViewsQueryAlter;
Chris@4 21
Chris@4 22 /**
Chris@4 23 * Implements hook_help().
Chris@4 24 */
Chris@4 25 function workspaces_help($route_name, RouteMatchInterface $route_match) {
Chris@4 26 switch ($route_name) {
Chris@4 27 // Main module help for the Workspaces module.
Chris@4 28 case 'help.page.workspaces':
Chris@4 29 $output = '';
Chris@4 30 $output .= '<h3>' . t('About') . '</h3>';
Chris@4 31 $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>';
Chris@4 32 return $output;
Chris@4 33 }
Chris@4 34 }
Chris@4 35
Chris@4 36 /**
Chris@4 37 * Implements hook_entity_type_build().
Chris@4 38 */
Chris@4 39 function workspaces_entity_type_build(array &$entity_types) {
Chris@4 40 return \Drupal::service('class_resolver')
Chris@4 41 ->getInstanceFromDefinition(EntityTypeInfo::class)
Chris@4 42 ->entityTypeBuild($entity_types);
Chris@4 43 }
Chris@4 44
Chris@4 45 /**
Chris@4 46 * Implements hook_form_alter().
Chris@4 47 */
Chris@4 48 function workspaces_form_alter(&$form, FormStateInterface $form_state, $form_id) {
Chris@4 49 if ($form_state->getFormObject() instanceof EntityFormInterface) {
Chris@4 50 \Drupal::service('class_resolver')
Chris@4 51 ->getInstanceFromDefinition(EntityOperations::class)
Chris@4 52 ->entityFormAlter($form, $form_state, $form_id);
Chris@4 53 }
Chris@4 54 \Drupal::service('class_resolver')
Chris@4 55 ->getInstanceFromDefinition(FormOperations::class)
Chris@4 56 ->formAlter($form, $form_state, $form_id);
Chris@4 57 }
Chris@4 58
Chris@4 59 /**
Chris@4 60 * Implements hook_field_info_alter().
Chris@4 61 */
Chris@4 62 function workspaces_field_info_alter(&$definitions) {
Chris@4 63 \Drupal::service('class_resolver')
Chris@4 64 ->getInstanceFromDefinition(EntityTypeInfo::class)
Chris@4 65 ->fieldInfoAlter($definitions);
Chris@4 66 }
Chris@4 67
Chris@4 68 /**
Chris@5 69 * Implements hook_entity_preload().
Chris@4 70 */
Chris@5 71 function workspaces_entity_preload(array $ids, $entity_type_id) {
Chris@4 72 return \Drupal::service('class_resolver')
Chris@4 73 ->getInstanceFromDefinition(EntityOperations::class)
Chris@5 74 ->entityPreload($ids, $entity_type_id);
Chris@4 75 }
Chris@4 76
Chris@4 77 /**
Chris@4 78 * Implements hook_entity_presave().
Chris@4 79 */
Chris@4 80 function workspaces_entity_presave(EntityInterface $entity) {
Chris@4 81 return \Drupal::service('class_resolver')
Chris@4 82 ->getInstanceFromDefinition(EntityOperations::class)
Chris@4 83 ->entityPresave($entity);
Chris@4 84 }
Chris@4 85
Chris@4 86 /**
Chris@4 87 * Implements hook_entity_insert().
Chris@4 88 */
Chris@4 89 function workspaces_entity_insert(EntityInterface $entity) {
Chris@4 90 return \Drupal::service('class_resolver')
Chris@4 91 ->getInstanceFromDefinition(EntityOperations::class)
Chris@4 92 ->entityInsert($entity);
Chris@4 93 }
Chris@4 94
Chris@4 95 /**
Chris@4 96 * Implements hook_entity_update().
Chris@4 97 */
Chris@4 98 function workspaces_entity_update(EntityInterface $entity) {
Chris@4 99 return \Drupal::service('class_resolver')
Chris@4 100 ->getInstanceFromDefinition(EntityOperations::class)
Chris@4 101 ->entityUpdate($entity);
Chris@4 102 }
Chris@4 103
Chris@4 104 /**
Chris@4 105 * Implements hook_entity_predelete().
Chris@4 106 */
Chris@4 107 function workspaces_entity_predelete(EntityInterface $entity) {
Chris@4 108 return \Drupal::service('class_resolver')
Chris@4 109 ->getInstanceFromDefinition(EntityOperations::class)
Chris@4 110 ->entityPredelete($entity);
Chris@4 111 }
Chris@4 112
Chris@4 113 /**
Chris@4 114 * Implements hook_entity_access().
Chris@4 115 *
Chris@4 116 * @see \Drupal\workspaces\EntityAccess
Chris@4 117 */
Chris@4 118 function workspaces_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
Chris@4 119 return \Drupal::service('class_resolver')
Chris@4 120 ->getInstanceFromDefinition(EntityAccess::class)
Chris@4 121 ->entityOperationAccess($entity, $operation, $account);
Chris@4 122 }
Chris@4 123
Chris@4 124 /**
Chris@4 125 * Implements hook_entity_create_access().
Chris@4 126 *
Chris@4 127 * @see \Drupal\workspaces\EntityAccess
Chris@4 128 */
Chris@4 129 function workspaces_entity_create_access(AccountInterface $account, array $context, $entity_bundle) {
Chris@4 130 return \Drupal::service('class_resolver')
Chris@4 131 ->getInstanceFromDefinition(EntityAccess::class)
Chris@4 132 ->entityCreateAccess($account, $context, $entity_bundle);
Chris@4 133 }
Chris@4 134
Chris@4 135 /**
Chris@4 136 * Implements hook_views_query_alter().
Chris@4 137 */
Chris@4 138 function workspaces_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
Chris@4 139 return \Drupal::service('class_resolver')
Chris@4 140 ->getInstanceFromDefinition(ViewsQueryAlter::class)
Chris@4 141 ->alterQuery($view, $query);
Chris@4 142 }
Chris@4 143
Chris@4 144 /**
Chris@4 145 * Implements hook_cron().
Chris@4 146 */
Chris@4 147 function workspaces_cron() {
Chris@4 148 \Drupal::service('workspaces.manager')->purgeDeletedWorkspacesBatch();
Chris@4 149 }
Chris@4 150
Chris@4 151 /**
Chris@4 152 * Implements hook_toolbar().
Chris@4 153 */
Chris@4 154 function workspaces_toolbar() {
Chris@4 155 $items = [];
Chris@4 156 $items['workspace'] = [
Chris@4 157 '#cache' => [
Chris@4 158 'contexts' => [
Chris@4 159 'user.permissions',
Chris@4 160 ],
Chris@4 161 ],
Chris@4 162 ];
Chris@4 163
Chris@4 164 $current_user = \Drupal::currentUser();
Chris@4 165 if (!$current_user->hasPermission('administer workspaces')
Chris@4 166 && !$current_user->hasPermission('view own workspace')
Chris@4 167 && !$current_user->hasPermission('view any workspace')) {
Chris@4 168 return $items;
Chris@4 169 }
Chris@4 170
Chris@4 171 /** @var \Drupal\workspaces\WorkspaceInterface $active_workspace */
Chris@4 172 $active_workspace = \Drupal::service('workspaces.manager')->getActiveWorkspace();
Chris@4 173
Chris@4 174 $items['workspace'] += [
Chris@4 175 '#type' => 'toolbar_item',
Chris@4 176 'tab' => [
Chris@4 177 '#type' => 'link',
Chris@4 178 '#title' => $active_workspace->label(),
Chris@4 179 '#url' => $active_workspace->toUrl('collection'),
Chris@4 180 '#attributes' => [
Chris@4 181 'title' => t('Switch workspace'),
Chris@4 182 'class' => ['use-ajax', 'toolbar-icon', 'toolbar-icon-workspace'],
Chris@4 183 'data-dialog-type' => 'dialog',
Chris@4 184 'data-dialog-renderer' => 'off_canvas_top',
Chris@4 185 'data-dialog-options' => Json::encode([
Chris@4 186 'height' => 161,
Chris@4 187 'classes' => [
Chris@4 188 'ui-dialog' => 'workspaces-dialog',
Chris@4 189 ],
Chris@4 190 ]),
Chris@4 191 ],
Chris@4 192 '#cache' => ['tags' => $active_workspace->getCacheTags()],
Chris@4 193 ],
Chris@4 194 '#wrapper_attributes' => [
Chris@4 195 'class' => ['workspaces-toolbar-tab'],
Chris@4 196 ],
Chris@4 197 '#attached' => [
Chris@4 198 'library' => ['workspaces/drupal.workspaces.toolbar'],
Chris@4 199 ],
Chris@4 200 '#weight' => 500,
Chris@4 201 ];
Chris@4 202
Chris@4 203 // Add a special class to the wrapper if we are in the default workspace so we
Chris@4 204 // can highlight it with a different color.
Chris@4 205 if ($active_workspace->isDefaultWorkspace()) {
Chris@4 206 $items['workspace']['#wrapper_attributes']['class'][] = 'workspaces-toolbar-tab--is-default';
Chris@4 207 }
Chris@4 208
Chris@4 209 return $items;
Chris@4 210 }