In your route, instead of _permission
, you can use a custom access callback:
_custom_access: Drupal\[MODULE]\Controller::accessCallback()
Then you can return an AccessResult from the access callback:
public function accessCallback($entity_type, $entity_id) {
if (!in_array($entity_type, $allowed_types)) {
return \Drupal\Core\Access\AccessResult::forbidden();
}
...
return \Drupal\Core\Access\AAccessResult::neutral();
}
Or, you can use a _permission
on the route, and instead throw a Symfony\Component\HttpKernel\Exception\NotFoundHttpException
exception from the controller if the values are incorrect:
public function accessCallbacksomeRouteCallback($entity_type, $entity_id) {
if (!in_array$entity = \Drupal::entityTypeManager()->getStorage($entity_type, $allowed_types)->load($entity_id)) {
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
...
}