You are here

public function LearningPathEventSubscriber::moduleRedirect in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/EventSubscriber/LearningPathEventSubscriber.php \Drupal\opigno_learning_path\EventSubscriber\LearningPathEventSubscriber::moduleRedirect()

Redirect from canonical module path to module edit.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The route building event.

File

src/EventSubscriber/LearningPathEventSubscriber.php, line 66

Class

LearningPathEventSubscriber
Class LearningPathEventSubscriber.

Namespace

Drupal\opigno_learning_path\EventSubscriber

Code

public function moduleRedirect(FilterResponseEvent $event) {
  $route = \Drupal::routeMatch();
  $route_name = $route
    ->getRouteName();
  if ($route_name == 'entity.opigno_module.canonical') {

    // Get module from url.
    $module = $route
      ->getParameter('opigno_module');

    // Get target path.
    $url = Url::fromRoute('opigno_module.edit', [
      'opigno_module' => $module
        ->id(),
    ])
      ->toString();

    // Set redirect response.
    $response = new RedirectResponse($url);
    $event
      ->setResponse($response);
  }
}