Problem/Motivation

Symfony\Component\Routing\Exception\RouteNotFoundException: Route "entity.storage.translation_revert" does not exist. in Drupal\Core\Routing\RouteProvider->getRouteByName() (line 206 of core/lib/Drupal/Core/Routing/RouteProvider.php).

Steps to reproduce

When trying to view the revisions of any storage entity with translation, a fatal error is displayed

Comments

freenjt created an issue. See original summary.

freenjt’s picture

Component: Code » User interface
Assigned: freenjt » Unassigned
freenjt’s picture

Creating a RouteSubscriber service that checks if the route exists, and if it does not exist, adds it, solves the problem.

class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if (!$this->routeExists('entity.storage.translation_revert')) {
      $this->routes($collection);
    }
  }

  /**
   * Function to add the translation_revert route of the Storage entities.
   */
  public function routes(RouteCollection $collection) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = $entity_type_manager->getDefinition('storage');
    if ($entity_type->hasLinkTemplate('translation_revert')) {
      $route = new Route($entity_type->getLinkTemplate('translation_revert'));
      $route
        ->setDefaults([
          '_form' => '\Drupal\storage\Form\StorageRevisionRevertTranslationForm',
          '_title' => 'Revert to earlier revision of a translation',
        ])
        ->setRequirement('_permission', 'revert all storage revisions')
        ->setOption('_admin_route', TRUE);
      $entity_type_id = $entity_type->id();
      $collection->add("entity.{$entity_type_id}.translation_revert", $route);
    }
  }

  /**
   * Check if rout exists.
   */
  private function routeExists($route_to_check) {
    try {
      \Drupal::service('router.route_provider')
        ->getRouteByName($route_to_check);
    }
    catch (RouteNotFoundException $exception) {
      return FALSE;
    }
    return TRUE;
  }

}
freenjt’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

freenjt’s picture