5 * Provides full-site preview functionality for content staging.
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;
23 * Implements hook_help().
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':
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>';
37 * Implements hook_entity_type_build().
39 function workspaces_entity_type_build(array &$entity_types) {
40 return \Drupal::service('class_resolver')
41 ->getInstanceFromDefinition(EntityTypeInfo::class)
42 ->entityTypeBuild($entity_types);
46 * Implements hook_form_alter().
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);
54 \Drupal::service('class_resolver')
55 ->getInstanceFromDefinition(FormOperations::class)
56 ->formAlter($form, $form_state, $form_id);
60 * Implements hook_field_info_alter().
62 function workspaces_field_info_alter(&$definitions) {
63 \Drupal::service('class_resolver')
64 ->getInstanceFromDefinition(EntityTypeInfo::class)
65 ->fieldInfoAlter($definitions);
69 * Implements hook_entity_load().
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);
78 * Implements hook_entity_presave().
80 function workspaces_entity_presave(EntityInterface $entity) {
81 return \Drupal::service('class_resolver')
82 ->getInstanceFromDefinition(EntityOperations::class)
83 ->entityPresave($entity);
87 * Implements hook_entity_insert().
89 function workspaces_entity_insert(EntityInterface $entity) {
90 return \Drupal::service('class_resolver')
91 ->getInstanceFromDefinition(EntityOperations::class)
92 ->entityInsert($entity);
96 * Implements hook_entity_update().
98 function workspaces_entity_update(EntityInterface $entity) {
99 return \Drupal::service('class_resolver')
100 ->getInstanceFromDefinition(EntityOperations::class)
101 ->entityUpdate($entity);
105 * Implements hook_entity_predelete().
107 function workspaces_entity_predelete(EntityInterface $entity) {
108 return \Drupal::service('class_resolver')
109 ->getInstanceFromDefinition(EntityOperations::class)
110 ->entityPredelete($entity);
114 * Implements hook_entity_access().
116 * @see \Drupal\workspaces\EntityAccess
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);
125 * Implements hook_entity_create_access().
127 * @see \Drupal\workspaces\EntityAccess
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);
136 * Implements hook_views_query_alter().
138 function workspaces_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
139 return \Drupal::service('class_resolver')
140 ->getInstanceFromDefinition(ViewsQueryAlter::class)
141 ->alterQuery($view, $query);
145 * Implements hook_cron().
147 function workspaces_cron() {
148 \Drupal::service('workspaces.manager')->purgeDeletedWorkspacesBatch();
152 * Implements hook_toolbar().
154 function workspaces_toolbar() {
156 $items['workspace'] = [
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')) {
171 /** @var \Drupal\workspaces\WorkspaceInterface $active_workspace */
172 $active_workspace = \Drupal::service('workspaces.manager')->getActiveWorkspace();
174 $items['workspace'] += [
175 '#type' => 'toolbar_item',
178 '#title' => $active_workspace->label(),
179 '#url' => $active_workspace->toUrl('collection'),
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([
188 'ui-dialog' => 'workspaces-dialog',
192 '#cache' => ['tags' => $active_workspace->getCacheTags()],
194 '#wrapper_attributes' => [
195 'class' => ['workspaces-toolbar-tab'],
198 'library' => ['workspaces/drupal.workspaces.toolbar'],
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';