SpecialAttributesRouteSubscriber.php 1.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber.
 */

namespace Drupal\Core\EventSubscriber;

use Drupal\Component\Utility\String;
use Drupal\Core\Routing\RouteBuildEvent;
12
use Drupal\Core\Routing\RouteSubscriberBase;
13
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
14
use Symfony\Component\Routing\RouteCollection;
15 16 17 18

/**
 * Provides a route subscriber which checks for invalid pattern variables.
 */
19
class SpecialAttributesRouteSubscriber extends RouteSubscriberBase {
20 21

  /**
22
   * {@inheritdoc}
23
   */
24
  protected function alterRoutes(RouteCollection $collection, $module) {
25 26 27 28 29 30 31 32 33 34 35 36
    $special_variables = array(
      'system_path',
      '_maintenance',
      '_legacy',
      '_authentication_provider',
      '_raw_variables',
      RouteObjectInterface::ROUTE_OBJECT,
      RouteObjectInterface::ROUTE_NAME,
      '_content',
      '_form',
    );

37
    foreach ($collection->all() as $route) {
38 39 40 41 42 43 44 45 46 47 48
      if ($not_allowed_variables = array_intersect($route->compile()->getVariables(), $special_variables)) {
        $placeholders = array('@variables' => implode(', ', $not_allowed_variables));
        drupal_set_message(String::format('The following variables are reserved names by drupal: @variables', $placeholders));
        watchdog('error', 'The following variables are reserved names by drupal: @variables', $placeholders);
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
49 50 51 52 53 54
   * Delegates the route altering to self::alterRoutes().
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The route build event.
   *
   * @return bool
55 56
   *   Returns TRUE if the variables were successfully replaced, otherwise
   *   FALSE.
57
   */
58 59
  public function onAlterRoutes(RouteBuildEvent $event) {
    $collection = $event->getRouteCollection();
60
    return $this->alterRoutes($collection, $event->getProvider());
61 62 63
  }

}