workspaces.module 7.3 KB

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