arch-8.x-1.x-dev/modules/product/arch_product.install

modules/product/arch_product.install
<?php

/**
 * @file
 * Install, update and uninstall functions for the arch module.
 */

use Drupal\Core\Url;
use Drupal\user\RoleInterface;

/**
 * Implements hook_requirements().
 */
function arch_product_requirements($phase) {
  $requirements = [];
  if ($phase === 'runtime') {
    // Only show rebuild button if there are either 0, or 2 or more, rows
    // in the {arch_product_access} table, or if there are modules that
    // implement hook_product_grants().
    $grant_count = \Drupal::entityTypeManager()
      ->getAccessControlHandler('product')
      ->countGrants();
    if (
      $grant_count != 1
      || \Drupal::moduleHandler()->hasImplementations('product_grants')
    ) {
      $value = \Drupal::translation()->formatPlural(
        $grant_count,
        'One permission in use',
        '@count permissions in use',
        ['@count' => $grant_count]
      );
    }
    else {
      $value = t('Disabled');
    }

    $requirements['product_access'] = [
      'title' => t('Product Access Permissions'),
      'value' => $value,
      'description' => t('If the site is experiencing problems with permissions to products, you may have to rebuild the permissions cache. Rebuilding will remove all privileges to products and replace them with permissions based on the current modules and settings. Rebuilding may take some time if there is a lot of product or complex permission settings. After rebuilding has completed, products will automatically use the new permissions. <a href=":rebuild">Rebuild permissions</a>', [
        ':rebuild' => Url::fromRoute('arch.product.configure_rebuild_confirm')->toString(),
      ]),
    ];
  }
  return $requirements;
}

/**
 * Implements hook_schema().
 */
function arch_product_schema() {
  $schema['arch_product_access'] = [
    'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific products.',
    'fields' => [
      'pid' => [
        'description' => 'The {arch_product}.pid this record affects.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'langcode' => [
        'description' => 'The {language}.langcode of this product.',
        'type' => 'varchar_ascii',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ],
      'fallback' => [
        'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
      ],
      'gid' => [
        'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the product.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'realm' => [
        'description' => 'The realm in which the user must possess the grant ID. Modules can define one or more realms by implementing hook_product_grants().',
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'grant_view' => [
        'description' => 'Boolean indicating whether a user with the realm/grant pair can view this product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ],
      'grant_update' => [
        'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ],
      'grant_delete' => [
        'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ],
    ],
    'primary key' => ['pid', 'gid', 'realm', 'langcode'],
    'foreign keys' => [
      'affected_product' => [
        'table' => 'arch_product',
        'columns' => ['pid' => 'pid'],
      ],
    ],
  ];

  return $schema;
}

/**
 * Implements hook_install().
 */
function arch_product_install() {
  // Enable default permissions for system roles.
  // IMPORTANT: Modules SHOULD NOT automatically grant any user role access
  // permissions in hook_install().
  // However, the 'access products' permission is a very special case, since
  // there is hardly a point in installing the Arch module without granting
  // these permissions. Doing so also allows tests to continue to operate as
  // expected without first having to manually grant these default permissions.
  if (\Drupal::moduleHandler()->moduleExists('user')) {
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access products']);
    user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access products']);
  }

  // Populate the product access table.
  \Drupal::database()->insert('arch_product_access')
    ->fields([
      'pid' => 0,
      'gid' => 0,
      'realm' => 'all',
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
    ])
    ->execute();
}

/**
 * Implements hook_uninstall().
 */
function arch_product_uninstall() {
  // Delete remaining general module variables.
  \Drupal::state()->delete('product.product_access_needs_rebuild');
}

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

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