You are here

class RouterPathTranslatorSubscriber in Decoupled Router 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/RouterPathTranslatorSubscriber.php \Drupal\decoupled_router\EventSubscriber\RouterPathTranslatorSubscriber

Event subscriber that processes a path translation with the router info.

Hierarchy

Expanded class hierarchy of RouterPathTranslatorSubscriber

1 string reference to 'RouterPathTranslatorSubscriber'
decoupled_router.services.yml in ./decoupled_router.services.yml
decoupled_router.services.yml
1 service uses RouterPathTranslatorSubscriber
decoupled_router.router_path_translator.subscriber in ./decoupled_router.services.yml
Drupal\decoupled_router\EventSubscriber\RouterPathTranslatorSubscriber

File

src/EventSubscriber/RouterPathTranslatorSubscriber.php, line 30

Namespace

Drupal\decoupled_router\EventSubscriber
View source
class RouterPathTranslatorSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The service container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * The logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The router.
   *
   * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface
   */
  protected $router;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The config factory
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The alias manager
   *
   * @var \Drupal\path_alias\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * RouterPathTranslatorSubscriber constructor.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger.
   * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $router
   *   The router.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory
   * @param \Drupal\path_alias\AliasManagerInterface $aliasManager
   *   The alias manager
   */
  public function __construct(ContainerInterface $container, LoggerInterface $logger, UrlMatcherInterface $router, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, AliasManagerInterface $aliasManager) {
    $this->container = $container;
    $this->logger = $logger;
    $this->router = $router;
    $this->moduleHandler = $module_handler;
    $this->configFactory = $config_factory;
    $this->aliasManager = $aliasManager;
  }

  /**
   * Processes a path translation request.
   */
  public function onPathTranslation(PathTranslatorEvent $event) {
    $response = $event
      ->getResponse();
    if (!$response instanceof CacheableJsonResponse) {
      $this->logger
        ->error('Unable to get the response object for the decoupled router event.');
      return;
    }
    $path = $event
      ->getPath();
    $path = $this
      ->cleanSubdirInPath($path, $event
      ->getRequest());
    try {
      $match_info = $this->router
        ->match($path);
    } catch (ResourceNotFoundException $exception) {

      // If URL is external, we won't perform checks for content in Drupal,
      // but assume that it's working.
      if (UrlHelper::isExternal($path)) {
        $response
          ->setStatusCode(200);
        $response
          ->setData(array(
          'resolved' => $path,
        ));
      }
      return;
    } catch (MethodNotAllowedException $exception) {
      $response
        ->setStatusCode(403);
      return;
    }

    /** @var \Drupal\Core\Entity\EntityInterface $entity */

    /** @var bool $param_uses_uuid */
    list($entity, $param_uses_uuid, $route_parameter_entity_key) = $this
      ->findEntityAndKeys($match_info);
    if (!$entity) {
      $this->logger
        ->notice('A route has been found but it has no entity information.');
      return;
    }
    $response
      ->addCacheableDependency($entity);
    $can_view = $entity
      ->access('view', NULL, TRUE);
    if (!$can_view
      ->isAllowed()) {
      $response
        ->setData([
        'message' => 'Access denied for entity.',
        'details' => 'This user does not have access to view the resolved entity. Please authenticate and try again.',
      ]);
      $response
        ->setStatusCode(403);
      $response
        ->addCacheableDependency($can_view);
      return;
    }
    $entity_type_id = $entity
      ->getEntityTypeId();
    $canonical_url = NULL;
    try {
      $canonical_url = $entity
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString(TRUE);
    } catch (EntityMalformedException $e) {
      $response
        ->setData([
        'message' => 'Unable to build entity URL.',
        'details' => 'A valid entity was found but it was impossible to generate a valid canonical URL for it.',
      ]);
      $response
        ->setStatusCode(500);
      watchdog_exception('decoupled_router', $e);
      return;
    }
    $entity_param = $param_uses_uuid ? $entity
      ->id() : $entity
      ->uuid();
    $resolved_url = Url::fromRoute($match_info[RouteObjectInterface::ROUTE_NAME], [
      $route_parameter_entity_key => $entity_param,
    ], [
      'absolute' => TRUE,
    ])
      ->toString(TRUE);
    $response
      ->addCacheableDependency($canonical_url);
    $response
      ->addCacheableDependency($resolved_url);
    $is_home_path = $this
      ->resolvedPathIsHomePath($resolved_url
      ->getGeneratedUrl());
    $response
      ->addCacheableDependency((new CacheableMetadata())
      ->setCacheContexts([
      'url.path.is_front',
    ]));
    $label_accessible = $entity
      ->access('view label', NULL, TRUE);
    $response
      ->addCacheableDependency($label_accessible);
    $output = [
      'resolved' => $resolved_url
        ->getGeneratedUrl(),
      'isHomePath' => $is_home_path,
      'entity' => [
        'canonical' => $canonical_url
          ->getGeneratedUrl(),
        'type' => $entity_type_id,
        'bundle' => $entity
          ->bundle(),
        'id' => $entity
          ->id(),
        'uuid' => $entity
          ->uuid(),
      ],
    ];
    if ($label_accessible
      ->isAllowed()) {
      $output['label'] = $entity
        ->label();
    }

    // If the route is JSON API, it means that JSON API is installed and its
    // services can be used.
    if ($this->moduleHandler
      ->moduleExists('jsonapi')) {

      /** @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $rt_repo */
      $rt_repo = $this->container
        ->get('jsonapi.resource_type.repository');
      $rt = $rt_repo
        ->get($entity_type_id, $entity
        ->bundle());
      $type_name = $rt
        ->getTypeName();
      $jsonapi_base_path = $this->container
        ->getParameter('jsonapi.base_path');
      $entry_point_url = Url::fromRoute('jsonapi.resource_list', [], [
        'absolute' => TRUE,
      ])
        ->toString(TRUE);
      $route_name = sprintf('jsonapi.%s.individual', $type_name);
      $individual = Url::fromRoute($route_name, [
        static::getEntityRouteParameterName($route_name, $entity_type_id) => $entity
          ->uuid(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString(TRUE);
      $response
        ->addCacheableDependency($entry_point_url);
      $response
        ->addCacheableDependency($individual);
      $output['jsonapi'] = [
        'individual' => $individual
          ->getGeneratedUrl(),
        'resourceName' => $type_name,
        'pathPrefix' => trim($jsonapi_base_path, '/'),
        'basePath' => $jsonapi_base_path,
        'entryPoint' => $entry_point_url
          ->getGeneratedUrl(),
      ];
      $deprecation_message = 'This property has been deprecated and will be removed in the next version of Decoupled Router. Use @alternative instead.';
      $output['meta'] = [
        'deprecated' => [
          'jsonapi.pathPrefix' => $this
            ->t($deprecation_message, [
            '@alternative' => 'basePath',
          ]),
        ],
      ];
    }
    $response
      ->addCacheableDependency($entity);
    $response
      ->setStatusCode(200);
    $response
      ->setData($output);
    $event
      ->stopPropagation();
  }

  /**
   * Get the underlying entity and the type of ID param enhancer for the routes.
   *
   * @param array $match_info
   *   The router match info.
   *
   * @return array
   *   The pair of \Drupal\Core\Entity\EntityInterface and bool with the
   *   underlying entity and the info weather or not it uses UUID for the param
   *   enhancement. It also returns the name of the parameter under which the
   *   entity lives in the route ('node' vs 'entity').
   */
  protected function findEntityAndKeys(array $match_info) {
    $entity = NULL;

    /** @var \Symfony\Component\Routing\Route $route */
    $route = $match_info[RouteObjectInterface::ROUTE_OBJECT];
    $route_parameters = $route
      ->getOption('parameters');
    $route_parameter_entity_key = 'entity';
    if (!empty($match_info['entity']) && $match_info['entity'] instanceof EntityInterface) {
      $entity = $match_info['entity'];
    }
    else {
      $entity_type_id = $this
        ->findEntityTypeFromRoute($route);

      /** @var \Drupal\Core\Entity\EntityInterface $entity */

      // TODO: $match_info[$entity_type_id] is broken for JSON API 2.x routes.
      // Now it will be $match_info[$entity_type_id] for core and
      // $match_info['entity'] for JSON API :-(
      if (!empty($entity_type_id) && !empty($match_info[$entity_type_id]) && $match_info[$entity_type_id] instanceof EntityInterface) {
        $route_parameter_entity_key = $entity_type_id;
        $entity = $match_info[$entity_type_id];
      }
    }
    $param_uses_uuid = strpos($route_parameters[$route_parameter_entity_key]['converter'], 'entity_uuid') === FALSE;
    return [
      $entity,
      $param_uses_uuid,
      $route_parameter_entity_key,
    ];
  }

  /**
   * Computes the name of the entity route parameter for JSON API routes.
   *
   * @param string $route_name
   *   A JSON API route name.
   * @param string $entity_type_id
   *   The corresponding entity type ID.
   *
   * @return string
   *   Either 'entity' or $entity_type_id.
   *
   * @todo Remove this once decoupled_router requires jsonapi >= 8.x-2.0.
   */
  protected static function getEntityRouteParameterName($route_name, $entity_type_id) {
    static $first;
    if (!isset($first)) {
      $route_parameters = \Drupal::service('router.route_provider')
        ->getRouteByName($route_name)
        ->getOption('parameters');
      $first = isset($route_parameters['entity']) ? 'entity' : $entity_type_id;
      return $first;
    }
    return $first === 'entity' ? 'entity' : $entity_type_id;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[PathTranslatorEvent::TRANSLATE][] = [
      'onPathTranslation',
    ];
    return $events;
  }

  /**
   * Extracts the entity type for the route parameters.
   *
   * If there are more than one parameter, this function will return the first
   * one.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route.
   *
   * @return string|null
   *   The entity type ID or NULL if not found.
   */
  protected function findEntityTypeFromRoute(Route $route) {
    $parameters = (array) $route
      ->getOption('parameters');

    // Find the entity type for the first parameter that has one.
    return array_reduce($parameters, function ($carry, $parameter) {
      if (!$carry && !empty($parameter['type'])) {
        $parts = explode(':', $parameter['type']);

        // We know that the parameter is for an entity if the type is set to
        // 'entity:<entity-type-id>'.
        if ($parts[0] === 'entity' && !empty($parts[1])) {
          $carry = $parts[1];
        }
      }
      return $carry;
    }, NULL);
  }

  /**
   * Removes the subdir prefix from the path.
   *
   * @param $path
   *   The path that can contain the subdir prefix.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request to extract the path prefix from.
   *
   * @return string
   *   The clean path.
   */
  protected function cleanSubdirInPath($path, Request $request) {

    // Remove any possible leading subdir information in case Drupal is
    // installed under http://example.com/d8/index.php
    $regexp = preg_quote($request
      ->getBasePath(), '/');
    return preg_replace(sprintf('/^%s/', $regexp), '', $path);
  }

  /**
   * Checks if the resolved path is the home path.
   *
   * @param string $resolved_url
   *   The resolved url from the request.
   *
   * @return bool
   *   True if the resolved path is the home path, false otherwise.
   */
  protected function resolvedPathIsHomePath($resolved_url) {
    $home_path = $this->configFactory
      ->get('system.site')
      ->get('page.front');
    $home_url = Url::fromUserInput($home_path, [
      'absolute' => TRUE,
    ])
      ->toString(TRUE)
      ->getGeneratedUrl();
    return $resolved_url === $home_url;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouterPathTranslatorSubscriber::$aliasManager protected property The alias manager
RouterPathTranslatorSubscriber::$configFactory protected property The config factory
RouterPathTranslatorSubscriber::$container protected property The service container.
RouterPathTranslatorSubscriber::$logger protected property The logger.
RouterPathTranslatorSubscriber::$moduleHandler protected property The module handler.
RouterPathTranslatorSubscriber::$router protected property The router.
RouterPathTranslatorSubscriber::cleanSubdirInPath protected function Removes the subdir prefix from the path.
RouterPathTranslatorSubscriber::findEntityAndKeys protected function Get the underlying entity and the type of ID param enhancer for the routes.
RouterPathTranslatorSubscriber::findEntityTypeFromRoute protected function Extracts the entity type for the route parameters.
RouterPathTranslatorSubscriber::getEntityRouteParameterName protected static function Computes the name of the entity route parameter for JSON API routes.
RouterPathTranslatorSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 1
RouterPathTranslatorSubscriber::onPathTranslation public function Processes a path translation request. 1
RouterPathTranslatorSubscriber::resolvedPathIsHomePath protected function Checks if the resolved path is the home path.
RouterPathTranslatorSubscriber::__construct public function RouterPathTranslatorSubscriber constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.