Skip to content

Instantly share code, notes, and snippets.

@aneek
Created September 18, 2015 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneek/b4ecd71f357ca47039b0 to your computer and use it in GitHub Desktop.
Save aneek/b4ecd71f357ca47039b0 to your computer and use it in GitHub Desktop.
Routing alter D8
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
$front_page = \Drupal::config('system.site')->get('page.front');
// Not getting the front value with the below process (followed: https://www.drupal.org/node/2187643)
$route = $collection->get('page.front');
\Drupal::logger('mod')->notice(var_export($route, TRUE)); // returns NULL
\Drupal::logger('mod1')->notice(var_export($front_page, TRUE)); // returns the proper value.
foreach ($collection as $route) {
if ($route->getPath() == $front_page) {
\Drupal::logger('mod2')->notice(var_export($route->getPath(), TRUE));
$route->setDefaults(['_content' => [$this, 'content']]);
break;
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
// Ensure to run after the views route subscriber.
// @see \Drupal\views\EventSubscriber
$events[RoutingEvents::ALTER] = ['onAlterRoutes', -180];
return $events;
}
/**
* Content callback.
*/
public static function content() {
return ['#markup' => ''];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment