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