🇨🇦Canada @freenjt

Vancouver
Account created on 31 January 2014, almost 11 years ago
#

Recent comments

🇨🇦Canada freenjt Vancouver

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;
  }

}
Production build 0.71.5 2024