Created
September 18, 2015 08:28
-
-
Save aneek/b4ecd71f357ca47039b0 to your computer and use it in GitHub Desktop.
Routing alter D8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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