1

I have followed this documentation page : https://www.drupal.org/docs/drupal-apis/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones in attempt to alter access of translation overview page ie: www.example.com/node/1/translations

<?php

namespace Drupal\link_enhancements\Routing;

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

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

  /**
   * {@inheritdoc}
   */
  public function alterRoutes(RouteCollection $collection) {

    $route_name = "entity.node.content_translation_overview";
    var_dump($collection->get($route_name)); exit;
    if ($route = $collection->get($route_name)) {
      $requirements = $route->getRequirements();
      unset($requirements['_permission']);
      // if both role then only we will allow ...code will go here.....
      $route->setRequirement('_access', 'FALSE');
    }

  }

}


services:
  link_enhancements.route_subscriber:
    class: Drupal\link_enhancements\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

But below route output is null and similar code works for other routes but requirement to alter translation overview route which returns null.

$route_name = "entity.node.content_translation_overview";
    var_dump($collection->get($route_name)); exit;

core/modules/content_translation/src/Routing/ContentTranslationRouteSubscriber.php

route is added in core module by below code and I have searched and tried but not sure how can we alter such routes, please suggest.

if ($entity_type->hasLinkTemplate('drupal:content-translation-overview')) {
        $route = new Route(
          $entity_type->getLinkTemplate('drupal:content-translation-overview'),
          [
            '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
            'entity_type_id' => $entity_type_id,
          ],
          [
            '_entity_access' => $entity_type_id . '.view',
            '_access_content_translation_overview' => $entity_type_id,
          ],
          [
            'parameters' => [
              $entity_type_id => [
                'type' => 'entity:' . $entity_type_id,
                'load_latest_revision' => $load_latest_revision,
              ],
            ],
            '_admin_route' => $is_admin,
          ]
        );
        $route_name = "entity.$entity_type_id.content_translation_overview";
        $collection->add($route_name, $route);
      }

1 Answer 1

1

It was not working because in content_transaltion module priority was set to -210 so make need to change priority of custom module to -220 and it started working.

public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = ['onAlterRoutes', -220];
    return $events;
  }

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.