You are here

workspace.install in Workspace 8.2

Same filename and directory in other branches
  1. 8 workspace.install
  2. 6 workspace.install
  3. 7 workspace.install

Contains install, update and uninstall functions for the workspace module.

File

workspace.install
View source
<?php

/**
 * @file
 * Contains install, update and uninstall functions for the workspace module.
 */
use Drupal\workspace\Entity\Workspace;

/**
 * Implements hook_install().
 */
function workspace_install() {

  // Set the owner of these default workspaces to be first user which which has
  // the 'administrator' role. This way we avoid hard coding user ID 1 for sites
  // that prefer to not give it any special meaning.
  $admin_roles = \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->getQuery()
    ->condition('is_admin', TRUE)
    ->execute();
  if (!empty($admin_roles)) {
    $query = \Drupal::entityTypeManager()
      ->getStorage('user')
      ->getQuery()
      ->condition('roles', $admin_roles, 'IN')
      ->condition('status', 1)
      ->sort('uid', 'ASC')
      ->range(0, 1);
    $result = $query
      ->execute();
  }

  // Default to user ID 1 if we could not find any other administrator users.
  $owner_id = !empty($result) ? reset($result) : 1;

  // Create two workspaces by default, 'live' and 'stage'.
  Workspace::create([
    'id' => 'live',
    'label' => 'Live',
    'target' => '',
    'uid' => $owner_id,
  ])
    ->save();
  Workspace::create([
    'id' => 'stage',
    'label' => 'Stage',
    'target' => 'live',
    'uid' => $owner_id,
  ])
    ->save();
}

Functions

Namesort descending Description
workspace_install Implements hook_install().