0

I am able to change permission callback perfectly for static urls: eg: Works fine:

if ($route = $collection->get('entity.taxonomy_vocabulary.collection')) {
  foreach ($vocabularies as $vocabulary) {
    $perms['administer ' . $vocabulary->id() . ' vocabulary terms'] = array(
      'title' => t('Administer %name vocabulary terms', array('%name' => $vocabulary->label())),
    );
    $route->setRequirement('_permission', 'administer ' . $vocabulary->id() . ' vocabulary terms');
  }
}

But same logic does not work for dynamic routes i.e route having a variable say taxonomy machine name.

Does not work:

if ($route = $collection->get('entity.taxonomy_term.add_form')) {
        foreach ($vocabularies as $vocabulary) {
            if($taxonomy_vocabulary == $vocabulary->id) {
                $route->setRequirement('_permission', 'administer ' . $vocabulary->id() . ' vocabulary terms');
            }
        }
      }

Please suggest how to alter/change permission callback for taxonomy add/view etc routes.

3

The _permission route setting isn't a callback function like the access callback in Drupal 7; it defines one or more permissions that are checked before granting access to the route. There's nothing dynamic about the setting. Adding one (or more) 'administer taxonomy_id vocabulary terms' permissions will restrict access if the user doesn't have those one or more permissions.

If you need to define a dynamic access check, you'll need to utilize _custom_access and define your own callback function that takes into account a specific permission check based on the context (see Structure of routes for more information).

  • It still does not work. I tried services: access_check.entity_create: class: Drupal\my_module\Access\CustomAccessCheck arguments: ['@current_user'] tags: - { name: access_check, applies_to: _entity_create_access } & public function access(AccountInterface $account) { return $account->hasPermission('Administer tags vocabulary terms'); – neha Aug 16 '16 at 11:13
  • Troubleshooting from comments won't help much here, but I'll mention 2 things. You still need a RouteSubscriber:: alterRoutes function to add the your _custom_access function (not a yml definition). Also the_custom_access function needs to return an AccessResult derived class, not a bool. See the referenced link for more information. – Shawn Conn Aug 16 '16 at 19:44
  • I have tried services: access_check.my_module_taxonomy: class: Drupal\my_module\Access\CustomAccessCheck arguments: ['@current_user'] tags: - { name: access_check} , also did $route->setRequirement('_access', 'TRUE'); inside route alter . Still does not work. I have almost tried everything. – neha Aug 18 '16 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.