You are here

protected function RouterPathTranslatorSubscriber::findEntityTypeFromRoute in Decoupled Router 8

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

Extracts the entity type for the route parameters.

If there are more than one parameter, this function will return the first one.

Parameters

\Symfony\Component\Routing\Route $route: The route.

Return value

string|null The entity type ID or NULL if not found.

1 call to RouterPathTranslatorSubscriber::findEntityTypeFromRoute()
RouterPathTranslatorSubscriber::findEntityAndKeys in src/EventSubscriber/RouterPathTranslatorSubscriber.php
Get the underlying entity and the type of ID param enhancer for the routes.

File

src/EventSubscriber/RouterPathTranslatorSubscriber.php, line 338

Class

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

Namespace

Drupal\decoupled_router\EventSubscriber

Code

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