You are here

class WebformRouteSubscriber in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Routing/WebformRouteSubscriber.php \Drupal\webform\Routing\WebformRouteSubscriber

Adds the _admin_route option to webform routes.

Hierarchy

Expanded class hierarchy of WebformRouteSubscriber

1 string reference to 'WebformRouteSubscriber'
webform.services.yml in ./webform.services.yml
webform.services.yml
1 service uses WebformRouteSubscriber
webform.route_subscriber in ./webform.services.yml
Drupal\webform\Routing\WebformRouteSubscriber

File

src/Routing/WebformRouteSubscriber.php, line 13

Namespace

Drupal\webform\Routing
View source
class WebformRouteSubscriber extends RouteSubscriberBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The configuration object factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a WebformShareRouteSubscriber object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration object factory.
   */
  public function __construct(ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory = NULL) {
    $this->moduleHandler = $module_handler;
    $this->configFactory = $config_factory ?: \Drupal::configFactory();
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {

    // Set admin route for webform admin routes.
    foreach ($collection
      ->all() as $route) {
      if (!$route
        ->hasOption('_admin_route') && (strpos($route
        ->getPath(), '/admin/structure/webform/') === 0 || strpos($route
        ->getPath(), '/webform/results/') !== FALSE)) {
        $route
          ->setOption('_admin_route', TRUE);
      }

      // Change /admin/structure/webform/ to /admin/webform/.
      if ($this->configFactory
        ->get('webform.settings')
        ->get('ui.toolbar_item')) {
        if (strpos($route
          ->getPath(), '/admin/structure/webform') === 0) {
          $path = str_replace('/admin/structure/webform', '/admin/webform', $route
            ->getPath());
          $route
            ->setPath($path);
        }
      }
    }

    // If the webform_share.module is not enabled, remove variant share route.
    if (!$this->moduleHandler
      ->moduleExists('webform_share')) {
      $collection
        ->remove('entity.webform.variant.share_form');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriberBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 5
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1
WebformRouteSubscriber::$configFactory protected property The configuration object factory.
WebformRouteSubscriber::$moduleHandler protected property The module handler.
WebformRouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
WebformRouteSubscriber::__construct public function Constructs a WebformShareRouteSubscriber object.