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