You are here

class RoutePathSubscriber in Tome 8

Adds route paths to the list of paths to export.

@internal

Hierarchy

  • class \Drupal\tome_static\EventSubscriber\RoutePathSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RoutePathSubscriber

1 string reference to 'RoutePathSubscriber'
tome_static.services.yml in modules/tome_static/tome_static.services.yml
modules/tome_static/tome_static.services.yml
1 service uses RoutePathSubscriber
tome_static.route_path_subscriber in modules/tome_static/tome_static.services.yml
Drupal\tome_static\EventSubscriber\RoutePathSubscriber

File

modules/tome_static/src/EventSubscriber/RoutePathSubscriber.php, line 18

Namespace

Drupal\tome_static\EventSubscriber
View source
class RoutePathSubscriber implements EventSubscriberInterface {

  /**
   * The route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs the RoutePathSubscriber object.
   *
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(RouteProviderInterface $route_provider, LanguageManagerInterface $language_manager) {
    $this->routeProvider = $route_provider;
    $this->languageManager = $language_manager;
  }

  /**
   * Reacts to a collect paths event.
   *
   * @param \Drupal\tome_static\Event\CollectPathsEvent $event
   *   The collect paths event.
   */
  public function collectPaths(CollectPathsEvent $event) {
    $language_none = $this->languageManager
      ->getLanguage(LanguageInterface::LANGCODE_NOT_APPLICABLE);
    foreach ($this->routeProvider
      ->getAllRoutes() as $route_name => $route) {
      try {
        $url = Url::fromRoute($route_name, [], [
          'language' => $language_none,
        ]);
        $path = $url
          ->toString();
        if ($path && $url
          ->access()) {
          $event
            ->addPath(parse_url($path, PHP_URL_PATH));
        }
      } catch (\Exception $e) {
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[TomeStaticEvents::COLLECT_PATHS][] = [
      'collectPaths',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RoutePathSubscriber::$languageManager protected property The language manager.
RoutePathSubscriber::$routeProvider protected property The route provider.
RoutePathSubscriber::collectPaths public function Reacts to a collect paths event.
RoutePathSubscriber::getSubscribedEvents public static function
RoutePathSubscriber::__construct public function Constructs the RoutePathSubscriber object.