Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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_entity_load().
61  */
62 function workspaces_entity_load(array &$entities, $entity_type_id) {
63   return \Drupal::service('class_resolver')
64     ->getInstanceFromDefinition(EntityOperations::class)
65     ->entityLoad($entities, $entity_type_id);
66 }
67
68 /**
69  * Implements hook_entity_presave().
70  */
71 function workspaces_entity_presave(EntityInterface $entity) {
72   return \Drupal::service('class_resolver')
73     ->getInstanceFromDefinition(EntityOperations::class)
74     ->entityPresave($entity);
75 }
76
77 /**
78  * Implements hook_entity_insert().
79  */
80 function workspaces_entity_insert(EntityInterface $entity) {
81   return \Drupal::service('class_resolver')
82     ->getInstanceFromDefinition(EntityOperations::class)
83     ->entityInsert($entity);
84 }
85
86 /**
87  * Implements hook_entity_update().
88  */
89 function workspaces_entity_update(EntityInterface $entity) {
90   return \Drupal::service('class_resolver')
91     ->getInstanceFromDefinition(EntityOperations::class)
92     ->entityUpdate($entity);
93 }
94
95 /**
96  * Implements hook_entity_predelete().
97  */
98 function workspaces_entity_predelete(EntityInterface $entity) {
99   return \Drupal::service('class_resolver')
100     ->getInstanceFromDefinition(EntityOperations::class)
101     ->entityPredelete($entity);
102 }
103
104 /**
105  * Implements hook_entity_access().
106  *
107  * @see \Drupal\workspaces\EntityAccess
108  */
109 function workspaces_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
110   return \Drupal::service('class_resolver')
111     ->getInstanceFromDefinition(EntityAccess::class)
112     ->entityOperationAccess($entity, $operation, $account);
113 }
114
115 /**
116  * Implements hook_entity_create_access().
117  *
118  * @see \Drupal\workspaces\EntityAccess
119  */
120 function workspaces_entity_create_access(AccountInterface $account, array $context, $entity_bundle) {
121   return \Drupal::service('class_resolver')
122     ->getInstanceFromDefinition(EntityAccess::class)
123     ->entityCreateAccess($account, $context, $entity_bundle);
124 }
125
126 /**
127  * Implements hook_views_query_alter().
128  */
129 function workspaces_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
130   return \Drupal::service('class_resolver')
131     ->getInstanceFromDefinition(ViewsQueryAlter::class)
132     ->alterQuery($view, $query);
133 }
134
135 /**
136  * Implements hook_cron().
137  */
138 function workspaces_cron() {
139   \Drupal::service('workspaces.manager')->purgeDeletedWorkspacesBatch();
140 }
141
142 /**
143  * Implements hook_toolbar().
144  */
145 function workspaces_toolbar() {
146   $items = [];
147   $items['workspace'] = [
148     '#cache' => [
149       'contexts' => [
150         'user.permissions',
151       ],
152     ],
153   ];
154
155   $current_user = \Drupal::currentUser();
156   if (!$current_user->hasPermission('administer workspaces')
157     || !$current_user->hasPermission('view own workspace')
158     || !$current_user->hasPermission('view any workspace')) {
159     return $items;
160   }
161
162   /** @var \Drupal\workspaces\WorkspaceInterface $active_workspace */
163   $active_workspace = \Drupal::service('workspaces.manager')->getActiveWorkspace();
164
165   $items['workspace'] = [
166     '#type' => 'toolbar_item',
167     'tab' => [
168       '#type' => 'link',
169       '#title' => $active_workspace->label(),
170       '#url' => $active_workspace->toUrl('collection'),
171       '#attributes' => [
172         'title' => t('Switch workspace'),
173         'class' => ['use-ajax', 'toolbar-icon', 'toolbar-icon-workspace'],
174         'data-dialog-type' => 'dialog',
175         'data-dialog-renderer' => 'off_canvas_top',
176         'data-dialog-options' => Json::encode([
177           'height' => 161,
178           'classes' => [
179             'ui-dialog' => 'workspaces-dialog',
180           ],
181         ]),
182       ],
183     ],
184     '#wrapper_attributes' => [
185       'class' => ['workspaces-toolbar-tab'],
186     ],
187     '#attached' => [
188       'library' => ['workspaces/drupal.workspaces.toolbar'],
189     ],
190     '#weight' => 500,
191   ];
192
193   // Add a special class to the wrapper if we are in the default workspace so we
194   // can highlight it with a different color.
195   if ($active_workspace->isDefaultWorkspace()) {
196     $items['workspace']['#wrapper_attributes']['class'][] = 'workspaces-toolbar-tab--is-default';
197   }
198
199   return $items;
200 }