annotate core/modules/workspaces/workspaces.install @ 5:12f9dff5fda9 tip

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