You are here

function forum_access_install in Forum Access 8

Same name and namespace in other branches
  1. 5 forum_access.install \forum_access_install()
  2. 6 forum_access.install \forum_access_install()
  3. 7 forum_access.install \forum_access_install()

Implements hook_install().

@inheritdoc

Throws

\Exception

File

./forum_access.install, line 28
Forum Access install file.

Code

function forum_access_install() {

  // Set the weight of the forum_access.module to 2 so it
  // is loaded after the forum.module.
  module_set_weight('forum_access', 2);

  // Add default values for grants during installation.
  if ($vid = \Drupal::config('forum.settings')
    ->get('vocabulary')) {
    $terms = \Drupal::service('entity_type.manager')
      ->getStorage('taxonomy_term')
      ->loadTree($vid);
    $grants_rid_create = [
      AccountInterface::ANONYMOUS_ROLE => 0,
      AccountInterface::AUTHENTICATED_ROLE => 1,
    ];
    foreach ($terms as $term) {
      foreach ($grants_rid_create as $rid => $grant) {
        \Drupal::database()
          ->insert('forum_access')
          ->fields([
          'tid' => $term->tid,
          'rid' => $rid,
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'grant_create' => $grant,
          'priority' => 0,
        ])
          ->execute();
      }
    }
  }

  // Add integer accordance to roles. We need it for hook_node grants
  // which understands only integer as gid.
  $config = \Drupal::configFactory()
    ->getEditable('forum_access.settings');
  $roles_gids = [];

  /** @var Drupal\Core\Entity\EntityInterface[] $roles */
  $roles = Role::loadMultiple();
  $i = 1;
  foreach ($roles as $role) {
    $roles_gids[$role
      ->id()] = $i;
    $i++;
  }
  $config
    ->set('forum_access_roles_gids', $roles_gids);
  $config
    ->save();
}