taxonomy_permissions.module in Taxonomy Permissions 8
Same filename and directory in other branches
This module adds 'view' permissions to the Taxonomy core module.
File
taxonomy_permissions.moduleView source
<?php
/**
* @file
* taxonomy_permissions.module
*
* This module adds 'view' permissions to the Taxonomy core module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_help().
*/
function taxonomy_permissions_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.taxonomy_permissions':
$output = '';
$output .= '<h3>' . t("About") . '</h3>';
$output .= '<p>' . t("Taxonomy Permissions adds 'view terms in\n %vocabulary%' permissions to the list of permissions of the Taxonomy\n core module.") . '</p>';
$output .= '<p>' . t("To avoid surprises, vocabularies are visible by\n authenticated and anonymous users by default. You have to change the\n permissions to see any effect of this module.") . '</p>';
return $output;
default:
}
}
/**
* Implements hook_entity_type_alter().
*/
function taxonomy_permissions_entity_type_alter(array &$entity_types) {
/* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
$entity_types['taxonomy_term']
->setAccessClass('Drupal\\taxonomy_permissions\\TaxonomyPermissionsControlHandler');
}
/**
* Implements hook_entity_field_access().
*/
function taxonomy_permissions_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($field_definition
->getType() == 'entity_reference' && $field_definition
->getSetting('target_type') == 'taxonomy_term' && $operation == 'edit') {
$handler_settings = $field_definition
->getSetting('handler_settings');
$target_bundles = $handler_settings['target_bundles'];
// We grant access to the taxonomy reference field if the user has the
// permissions to view at least one vocabulary from the target bundles.
$access = FALSE;
foreach ($target_bundles as $vid => $target_bundle) {
if ($account
->hasPermission('view terms in ' . $vid)) {
$access = TRUE;
}
}
if ($access) {
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
}
return AccessResult::neutral();
}
/**
* Implements hook_entity_insert().
*/
function taxonomy_permissions_entity_insert(EntityInterface $entity) {
if ($entity
->getEntityTypeId() == 'taxonomy_vocabulary') {
$perms[] = 'view terms in ' . $entity
->id();
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, $perms);
user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, $perms);
}
}
/**
* Implements hook_entity_delete().
*/
function taxonomy_permissions_entity_delete(EntityInterface $entity) {
if ($entity
->getEntityTypeId() == 'taxonomy_vocabulary') {
$perms[] = 'view terms in ' . $entity
->id();
user_role_revoke_permissions(AccountInterface::ANONYMOUS_ROLE, $perms);
user_role_revoke_permissions(AccountInterface::AUTHENTICATED_ROLE, $perms);
}
}
Functions
Name | Description |
---|---|
taxonomy_permissions_entity_delete | Implements hook_entity_delete(). |
taxonomy_permissions_entity_field_access | Implements hook_entity_field_access(). |
taxonomy_permissions_entity_insert | Implements hook_entity_insert(). |
taxonomy_permissions_entity_type_alter | Implements hook_entity_type_alter(). |
taxonomy_permissions_help | Implements hook_help(). |