You are here

taxonomy_access_fix.install in Taxonomy access fix 8.2

Same filename and directory in other branches
  1. 8.3 taxonomy_access_fix.install
  2. 7.2 taxonomy_access_fix.install

Install, uninstall and update hooks for taxonomy_access_fix module.

File

taxonomy_access_fix.install
View source
<?php

/**
 * @file
 * Install, uninstall and update hooks for taxonomy_access_fix module.
 */
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\Role;

/**
 * Inform users about the new 'View terms in %vocabulary' permission.
 */
function taxonomy_access_fix_update_8201() {
  return t('A new per-vocabulary permission %permission_label has been introduced in @module_name 8.x-2.6. Users updating from prior versions may want to <a href=":permissions_url">grant this permission</a> to user roles, which should be able to view published Taxonomy Term entities.', [
    '%permission_label' => t('View terms in %vocabulary', [
      '%vocabulary' => t('Vocabulary label'),
    ]),
    '@module_name' => t('Taxonomy Access Fix'),
    ':permissions_url' => Url::fromRoute('user.admin_permissions')
      ->toString(),
  ]);
}

/**
 * Migrate from 'Add terms in %vocabulary' permission to Drupal Core permission.
 */
function taxonomy_access_fix_update_8202() {
  $vocabularies = Vocabulary::loadMultiple();
  foreach (Role::loadMultiple() as $role) {
    foreach ($vocabularies as $vocabulary) {
      $role
        ->grantPermission('view terms in ' . $vocabulary
        ->id())
        ->save();
      if ($role
        ->hasPermission('add terms in ' . $vocabulary
        ->id())) {
        $role
          ->grantPermission('create terms in ' . $vocabulary
          ->id());

        // Revoke the old permission so that it would be hidden, if it was
        // deprecated (@see Issue 2924785).
        $role
          ->revokePermission('add terms in ' . $vocabulary
          ->id());
        $role
          ->save();
      }
    }
  }
  return t('The "@old_permission_label" permissions provided by @module_name have been migrated to the new "@new_permission_label" permissions provided by Drupal Core. You may want to <a href=":permissions_url">review the updated permissions</a>.', [
    '@old_permission_label' => t('Add terms in %vocabulary', [
      '%vocabulary' => t('Vocabulary label'),
    ]),
    '@new_permission_label' => t('%vocabulary: Create terms', [
      '%vocabulary' => t('Vocabulary label'),
    ]),
    '@module_name' => t('Taxonomy Access Fix'),
    ':permissions_url' => Url::fromRoute('user.admin_permissions')
      ->toString(),
  ]);
}

Functions

Namesort descending Description
taxonomy_access_fix_update_8201 Inform users about the new 'View terms in %vocabulary' permission.
taxonomy_access_fix_update_8202 Migrate from 'Add terms in %vocabulary' permission to Drupal Core permission.