function workspaces_install

Same name in other branches
  1. 9 core/modules/workspaces/workspaces.install \workspaces_install()
  2. 8.9.x core/modules/workspaces/workspaces.install \workspaces_install()
  3. 11.x core/modules/workspaces/workspaces.install \workspaces_install()

Implements hook_install().

File

core/modules/workspaces/workspaces.install, line 52

Code

function workspaces_install() {
    // Set the owner of these default workspaces to be first user 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()
            ->accessCheck(FALSE)
            ->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 a 'stage' workspace by default.
    Workspace::create([
        'id' => 'stage',
        'label' => 'Stage',
        'uid' => $owner_id,
    ])->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.