workspaces.install 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @file
  4. * Contains install, update and uninstall functions for the Workspaces module.
  5. */
  6. use Drupal\workspaces\Entity\Workspace;
  7. /**
  8. * Implements hook_requirements().
  9. */
  10. function workspaces_requirements($phase) {
  11. $requirements = [];
  12. if ($phase === 'install') {
  13. if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
  14. $requirements['content_moderation_incompatibility'] = [
  15. 'severity' => REQUIREMENT_ERROR,
  16. 'description' => t('Workspaces can not be installed when Content Moderation is also installed.'),
  17. ];
  18. }
  19. if (\Drupal::moduleHandler()->moduleExists('workspace')) {
  20. $requirements['workspace_incompatibility'] = [
  21. 'severity' => REQUIREMENT_ERROR,
  22. 'description' => t('Workspaces can not be installed when the contributed Workspace module is also installed. See the <a href=":link">upgrade path</a> page for more information on how to upgrade.', [
  23. ':link' => 'https://www.drupal.org/node/2987783',
  24. ]),
  25. ];
  26. }
  27. }
  28. return $requirements;
  29. }
  30. /**
  31. * Implements hook_install().
  32. */
  33. function workspaces_install() {
  34. // Set the owner of these default workspaces to be first user which which has
  35. // the 'administrator' role. This way we avoid hard coding user ID 1 for sites
  36. // that prefer to not give it any special meaning.
  37. $admin_roles = \Drupal::entityTypeManager()->getStorage('user_role')->getQuery()
  38. ->condition('is_admin', TRUE)
  39. ->execute();
  40. if (!empty($admin_roles)) {
  41. $query = \Drupal::entityTypeManager()->getStorage('user')->getQuery()
  42. ->condition('roles', $admin_roles, 'IN')
  43. ->condition('status', 1)
  44. ->sort('uid', 'ASC')
  45. ->range(0, 1);
  46. $result = $query->execute();
  47. }
  48. // Default to user ID 1 if we could not find any other administrator users.
  49. $owner_id = !empty($result) ? reset($result) : 1;
  50. // Create two workspaces by default, 'live' and 'stage'.
  51. Workspace::create([
  52. 'id' => 'live',
  53. 'label' => 'Live',
  54. 'uid' => $owner_id,
  55. ])->save();
  56. Workspace::create([
  57. 'id' => 'stage',
  58. 'label' => 'Stage',
  59. 'uid' => $owner_id,
  60. ])->save();
  61. }