1

I have a custom route that needs to check if a user can create a certain group_node:article content inside a group (from the group module 8.x-1.0-rc2). The setting is as follows:

mymod.routing.yml

mymod.add:
  path: '/mymod/add/{group}/{plugin_id}'
  defaults:
    _controller: '\Drupal\mymod\Controller\MymodController::addNews'
    _title: 'Add news'
  requirements:
    _group_content_create_access: 'TRUE'

The group.services.yml

  access_check.group_content.create:
  class: 'Drupal\group\Access\GroupContentCreateAccessCheck'
  arguments: ['@entity.manager']
  tags:
    - { name: 'access_check', applies_to: '_group_content_create_access' }

GroupContentCreateAccessCheck.php

 /**
* Checks access for group content creation routes.
*
* All routes using this access check should have a group and plugin_id
* parameter and have the _group_content_create_access requirement set 
* to either 'TRUE' or 'FALSE'.
*
* @param \Symfony\Component\Routing\Route $route
*   The route to check against.
* @param \Drupal\Core\Session\AccountInterface $account
*   The currently logged in account.
* @param \Drupal\group\Entity\GroupInterface $group
*   The group in which the content should be created.
* @param string $plugin_id
*   The group content enabler ID to use for the group content entity.
*
* @return \Drupal\Core\Access\AccessResultInterface
*   The access result.
*/
public function access(Route $route, AccountInterface $account, GroupInterface $group, $plugin_id) {
  ...
}

From my understanding these 3 files should be enough to check _group_content_create_access. However, trying to access my route via

localhost/mysite/mymod/add/57/some-plugin-id

always results in RuntimeException: Callable "Drupal\group\Access\GroupContentCreateAccessCheck::access" requires a value for the "$group" argument.

What am I missing here?

UPDATE I tried to investigate this problem in more detail. The full error message is this one:

....requires a value for the "$group" argument. in 
Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() 
(line 148 of core/lib/Drupal/Component/Utility/ArgumentsResolver.php).   
Drupal\Component\Utility\ArgumentsResolver->getArgument(Object) (Line: 
54)
Drupal\Component\Utility\ArgumentsResolver->getArguments(Array) (Line: 
157)
Drupal\Core\Access\AccessManager- 
>performCheck('group_news.custom_access_check', Object) (Line: 135)
Drupal\Core\Access\AccessManager->check(Object, Object, Object, 1) 
(Line: 112)
Drupal\Core\Access\AccessManager->checkRequest(Object, Object, 1) 
(Line: 107)

The point where it fails in Drupal\Component\Utility\ArgumentsResolver->getArgument(..) is where is_object($this->objects[$parameter_name]) returns false, because my route parameter {group} that should be of type GroupInterface is actually of type String.

However, when testing another group module route that is not using a route-access check class, the {group} parameter gets noticed as GroupInterface. For example in

entity.group.leave:
  path: '/group/{group}/leave'

So the question points more in the general direction of how my route parameter '57' gets noticed as GroupInterface (or more general any specific type), because the same thing also fails when implementing my own little custom access check class (not using the group module at all).

0

Digging deeper into what causes the error message brought me to the drupal docs about Parameter upcasting in routes. When passing entity parameters to an access check function you need to upcast your parameter and tell Drupal of which entity type your argument is. I.e. in my_mod.routing.yml add options and argument type:

mymod.add:
  path: '/mymod/add/{group}/{plugin_id}'
  defaults:
    _controller: '\Drupal\mymod\Controller\MymodController::addNews'
    _title: 'Add news'
  requirements:
    _group_content_create_access: 'TRUE'
  options:
    parameters:
      group:
        type: entity:group

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.