You are here

class LearningPathEventSubscriber in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/LearningPathEventSubscriber.php \Drupal\opigno_learning_path\EventSubscriber\LearningPathEventSubscriber

Class LearningPathEventSubscriber.

Hierarchy

  • class \Drupal\opigno_learning_path\EventSubscriber\LearningPathEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LearningPathEventSubscriber

1 string reference to 'LearningPathEventSubscriber'
opigno_learning_path.services.yml in ./opigno_learning_path.services.yml
opigno_learning_path.services.yml
1 service uses LearningPathEventSubscriber
opigno_learning_path.event_subscriber in ./opigno_learning_path.services.yml
Drupal\opigno_learning_path\EventSubscriber\LearningPathEventSubscriber

File

src/EventSubscriber/LearningPathEventSubscriber.php, line 16

Namespace

Drupal\opigno_learning_path\EventSubscriber
View source
class LearningPathEventSubscriber implements EventSubscriberInterface {
  private $content_types_manager;

  /**
   * LearningPathEventSubscriber constructor.
   */
  public function __construct(LearningPathContentTypesManager $content_types_manager) {
    $this->content_types_manager = $content_types_manager;
  }

  /**
   * Returns an array of event names this subscriber wants to listen to.
   *
   * The array keys are event names and the value can be:
   *
   *  * The method name to call (priority defaults to 0)
   *  * An array composed of the method name to call and the priority
   *  * An array of arrays composed of the method names to call and respective
   *    priorities, or 0 if unset
   *
   * For instance:
   *
   *  * array('eventName' => 'methodName')
   *  * array('eventName' => array('methodName', $priority))
   *  * array('eventName' => array(array('methodName1', $priority),
   *  * array('methodName2')))
   *
   * @return array
   *   The event names to listen to.
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::REQUEST => [
        'onKernelRequest',
      ],
      KernelEvents::RESPONSE => [
        'moduleRedirect',
      ],
    ];
  }

  /**
   * Event called when a request is sent.
   */
  public function onKernelRequest(GetResponseEvent $event) {
  }

  /**
   * Redirect from canonical module path to module edit.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The route building event.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LearningPathEventSubscriber::$content_types_manager private property
LearningPathEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
LearningPathEventSubscriber::moduleRedirect public function Redirect from canonical module path to module edit.
LearningPathEventSubscriber::onKernelRequest public function Event called when a request is sent.
LearningPathEventSubscriber::__construct public function LearningPathEventSubscriber constructor.