forum_access-8.x-1.0-alpha2/forum_access.install

forum_access.install
<?php

/**
 * @file
 * Forum Access install file.
 */

use Drupal\Core\Database\Database;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\Role;

/**
 * Implements hook_schema().
 *
 * @inheritdoc
 */
function forum_access_schema() {
  return _forum_access_schema_8102();
}

/**
 * Implements hook_install().
 *
 * @inheritdoc
 *
 * @throws \Exception
 */
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();
}

/**
 * Updates for {forum_access} table.
 *
 * Resize {forum_access}.rid. and
 * add primary key and index to the {forum_access} table.
 */
function forum_access_update_8102(&$sandbox) {
  $new_schema = _forum_access_schema_8102();
  $schema = Database::getConnection()->schema();

  // Drop the primary key and index that may have been created in update_8101.
  $schema->dropPrimaryKey('forum_access');
  $schema->dropIndex('forum_access', 'rid');

  // Adjust the 'rid' field width.
  $schema->changeField('forum_access', 'rid', 'rid', $new_schema['forum_access']['fields']['rid']);

  // Add the primary key and index (again or for the first time).
  $schema->addPrimaryKey('forum_access', ['tid', 'rid']);
  $schema->addIndex('forum_access', 'rid', ['rid'], $new_schema['forum_access']);
}

/**
 * Implementation of forum_access_schema() version 8102.
 */
function _forum_access_schema_8102() {
  $schema['forum_access'] = [
    'description' => 'The Forum Access control table.',
    'fields' => [
      'tid' => [
        'description' => 'The {taxonomy_term_data}.tid to which this {forum_access} entry applies.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'rid' => [
        'description' => 'The {role}.rid to which this {forum_access} entry applies.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ],
      'grant_view' => [
        'description' => 'Whether to grant "view" permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'grant_update' => [
        'description' => 'Whether to grant "update" permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'grant_delete' => [
        'description' => 'Whether to grant "delete" permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'grant_create' => [
        'description' => 'Whether to grant "create" permission.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'priority' => [
        'description' => 'The priority of this grant.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => ['tid', 'rid'],
    'indexes' => ['rid' => ['rid']],
    'foreign keys' => [
      'tid' => ['taxonomy_term_data' => 'tid'],
      'rid' => ['user_roles' => 'rid'],
    ],
  ];
  return $schema;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc