1

On admin/content, I have created different views for articles and basic pages (the default content types created from Drupal). On admin/content, it shows Content | Article | Basic Page. When I already have Article and Basic page as separate.

On admin/content, I would like to show as Blank means no data instead of showing all data.

I tried the following code.

modules/custom/content/src/Routing/RouteSubscriber.php

namespace Drupal\content\Routing;

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

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * Used to alter routes.
   */
  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('system.admin_content')) {
      $route->setDefaults(array(
        '_controller' => '\Drupal\content\Controller\ContentOverviewController::overview',
      ));
    }
  }

}

I'm trying to override its default controller, so it show blank.

The code for the controller is the following one.

namespace Drupal\content\Controller;

use Drupal\system\Controller\SystemController;

/**
 * Class ContentOverviewController.
 */
class ContentOverviewController extends SystemController {

  /**
   * Content Overview.
   */
  public function overview($link_id) {

    $build = [
      '#markup' => $this->t('Hello World!'),
    ];
    return $build;
  }

}

The code doesn't seem to work.

I also tried to disable the content view, but that is not needed.

3
  • You can don't overwrite it
    – vinhdv
    Commented Aug 21, 2018 at 12:16
  • 1
    That's not true. You can disable this view, and make a new view with the same URL.
    – Kevin
    Commented Aug 21, 2018 at 19:43
  • Did you try add tag priority for your service? May be your service run before views.route_subscriber of view module.
    – MrD
    Commented Aug 27, 2018 at 5:41

2 Answers 2

1

As per documentation:

The \Drupal\example\Routing\RouteSubscriber::alterRoutes method is an event subscriber because it extends RouteSubscriberBase. Therefore, the class must be registered as an event subscriber service.

In your example you also need a content.services.yml:

services:
  content.route_subscriber:
    class: Drupal\content\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }
1
  • This worked for me. I had to extend a Controller and was missing the routing entry. Once I added it to *.routing.yml it worked. Thanks! Commented Mar 9, 2023 at 15:37
0

Check a namespace in your RouteSubscriber.php

You have

namespace Drupal\content\Routing

Should be

namespace Drupal\mcontent\Routing

Because your custom module is mcontent (not content)

1
  • Fixed the things
    – Mighty
    Commented Aug 21, 2018 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.