Quick Tip: Remove /node page in Drupal 8

Parent Feed: 

By default Drupal provides a /node page http://example.com/node that lists all content. Unless you use that page as your front page it's a good idea to disable it.

Go to Structure --> Views --> Frontpage (Content)



Then press the Save button. And boom, you're done!

Removing /node programmatically

You could also write a custom module that removes the route programmatically. This is a good idea if you want some more flexibility, like you want a route only to be enabled on development environment and you don't want to adjust configuration back and forth. So having a module for altering routes is a pretty good idea. We'll take a look at that another time, but here's how to achieve the same thing as above with a custom module:

If you're not familar with creating modules, you could start off by reading Creating custom modules in the official Drupal documentation.

So we'll start off by creating a new module: I'll call mine custom_dev and I'll put it in modules/custom/custom_dev

Next we'll tell Drupal about the module by creating a custom_dev.info.yml which contains:

name: Custom dev  
type: module  
description: Development module  
core: 8.x  
package: Custom  

To alter routes we'll have to define a RouteSubscriber which listens to route events.
Let's create a custom_dev.services.yml

    class: Drupal\custom_dev\Routing\RouteSubscriber
      - { name: event_subscriber }

Finally we'll create the RouteSubscriber class. So in custom_dev/src/Routing we'll create a RouteSubscriber.php file:


namespace Drupal\custom_dev\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;  
use Symfony\Component\Routing\RouteCollection;

 * Class RouteSubscriber.
 * @package Drupal\custom_dev\Routing
 * Listens to the dynamic route events.
class RouteSubscriber extends RouteSubscriberBase {  
   * {@inheritdoc}
  protected function alterRoutes(RouteCollection $collection) {
    // Remove /node page route.

That's it! Enable the module and /node will now be disabled.
If we would like to instead show a Permission denied, we could replace:

    // Remove /node page route.


// Deny /node page access.
if ($route = $collection->get('view.frontpage.page_1')) {  
    $route->setRequirement('_access', 'FALSE');

Learn more about Drupal 8 routing

Here are some good links to learn more about the routing system:

RSS Tags: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web