I am trying to override the entity view controller for a commerce product view, so that I can redirect the user based on certain conditions.
I am trying to do this through the RouteSubscriber::alterRoutes implementation.
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('entity.commerce_product.canonical')) {
$route->addDefaults(['_controller' => '\Drupal\mymodule\Controller\ProductViewController::view']);
}
}
}
My custom controller is acknowledged, but I'm getting the following error:
RuntimeException: Controller "Drupal\mymodule\Controller\ProductViewController::view()" requires that you provide a value for the "$_entity" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one. in Symfony\Component\HttpKernel\Controller\ArgumentResolver->getArguments() (line 78 of /var/www/html/vendor/symfony/http-kernel/Controller/ArgumentResolver.php).
My ProductViewController extends the Drupal\Core\Entity\Controller\EntityViewController.
class ProductViewController extends EntityViewController {
public function view(EntityInterface $_entity, $view_mode = 'full')
{
return parent::view($_entity, $view_mode);
}
}
Can anyone tell what additional route context parameters I need to add to get this working?