workspaces.module 5.9 KB

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