You are here

social_book.install in Open Social 8.6

Install, update and uninstall functions for the social_book module.

File

modules/social_features/social_book/social_book.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the social_book module.
 */
use Drupal\user\Entity\Role;

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_book.
 */
function social_book_install() {

  // Set some default permissions.
  _social_book_set_permissions();
}

/**
 * Function to set permissions.
 */
function _social_book_set_permissions() {
  $roles = Role::loadMultiple();

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'administrator') {
      continue;
    }
    $permissions = _social_book_get_permissions($role
      ->id());
    user_role_grant_permissions($role
      ->id(), $permissions);
  }
}

/**
 * Build the permissions.
 *
 * @param string $role
 *   The role.
 *
 * @return array
 *   Returns an array containing the permissions.
 */
function _social_book_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [
    'view node.book.field_content_visibility:public content',
  ];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'view node.book.field_content_visibility:community content',
  ]);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['authenticated'], [
    'create new books',
    'add content to books',
    'create book content',
    'delete any book content',
    'delete own book content',
    'delete book revisions',
    'revert book revisions',
    'edit any book content',
    'edit own book content',
    'view book revisions',
  ]);

  // Site manager.
  $permissions['sitemanager'] = array_merge($permissions['contentmanager'], [
    'administer visibility settings',
  ]);
  if (isset($permissions[$role])) {
    return $permissions[$role];
  }
  return [];
}

/**
 * Enable book page creation permissions for CM and SM.
 */
function social_book_update_8001() {
  $roles = Role::loadMultiple();
  $permissions = [
    'create new books',
    'add content to books',
    'create book content',
    'delete any book content',
    'delete own book content',
    'delete book revisions',
    'revert book revisions',
    'edit any book content',
    'edit own book content',
    'view book revisions',
  ];

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'contentmanager' || $role
      ->id() === 'sitemanager') {
      user_role_grant_permissions($role
        ->id(), $permissions);
    }
  }
}

Functions

Namesort descending Description
social_book_install Implements hook_install().
social_book_update_8001 Enable book page creation permissions for CM and SM.
_social_book_get_permissions Build the permissions.
_social_book_set_permissions Function to set permissions.