1

I define a custom entity named "Politician", where I set the class PoliticianAccessControl as an access handler. This is working fine for the canonical url of the entity set in the following way:

entity.politician.canonical:
  path: '/politician/{politician}'
  defaults:
    _controller: '\Drupal\pw_basic\Controller\EntityViewController\PoliticianViewController::view'
    _title_callback: '\Drupal\pw_basic\Controller\EntityViewController\PoliticianViewController::title'
  requirements:
    _entity_access: 'politician.view'

See the entity access requirement? Here the method PoliticianAccessControl->checkAccess() ist called, everything works just fine.

But when I try the same with another custom route the PoliticianAccessControl class gets not called and I always get an access denied message:

entity.politician.about_me:
  path: '/politician/{politician}/about-me'
  defaults:
    _controller: '\Drupal\pw_basic\Controller\PoliticianFrontend\AboutMe::build'
    _title_callback: '\Drupal\pw_basic\Controller\PoliticianFrontend\AboutMe::title'
  requirements:
    _entity_access: 'politician.view'

What do I forget?

1
  • When I have a look at the controller AboutMe::build() where I expect the parameter $politician to be an Politician entity I see that no loaded entity arrives here but just the id, which is part of the route. Why does this happen here? The {politician} parameter is written in the same way as the entity type id. Jun 26, 2019 at 13:31

2 Answers 2

3

You need to give the router a nudge towards what $politician is supposed to be for the automatic up-casting to kick in. There are two ways to do that:

  1. Type-hint the variable in the controller method:

    function build(Politician $politician) {
    

    See EntityResolverManager::setParametersFromReflection for details

  2. Explicitly set the parameter type in the route config (which is pretty much what the first method does for you)

    options:
      parameters:
        politician:
          type: 'entity:politician'
    
3

I guess you need to do a route parameter upcasting:

entity.politician.about_me:
  path: '/politician/{politician}/about-me'
  defaults:
    _controller: '\Drupal\pw_basic\Controller\PoliticianFrontend\AboutMe::build'
    _title_callback: '\Drupal\pw_basic\Controller\PoliticianFrontend\AboutMe::title'
  requirements:
    _entity_access: 'politician.view'
  options:
    parameters:
      politician:
        type: entity:politician

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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