You are here

class EntityRevisionConverter in Workspace 8

Parameter converter for upcasting entity revision IDs to full objects.

Hierarchy

Expanded class hierarchy of EntityRevisionConverter

1 string reference to 'EntityRevisionConverter'
workspace.services.yml in ./workspace.services.yml
workspace.services.yml
1 service uses EntityRevisionConverter
workspace.paramconverter.entity_revision in ./workspace.services.yml
Drupal\workspace\ParamConverter\EntityRevisionConverter

File

src/ParamConverter/EntityRevisionConverter.php, line 16

Namespace

Drupal\workspace\ParamConverter
View source
class EntityRevisionConverter implements ParamConverterInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * Constructs a new EntityRevisionConverter.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
  }

  /**
   * {@inheritdoc}
   */
  public function convert($value, $definition, $name, array $defaults) {
    $entity_type_id = $this
      ->getEntityTypeFromDefaults($definition, $name, $defaults);
    if ($storage = $this->entityTypeManager
      ->getStorage($entity_type_id)) {
      $entity = $storage
        ->loadRevision($value);

      // If the entity type is translatable, ensure we return the proper
      // translation object for the current context.
      if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
        $entity = $this->entityRepository
          ->getTranslationFromContext($entity, NULL, [
          'operation' => 'entity_upcast',
        ]);
      }
      return $entity;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    if (!empty($definition['type']) && strpos($definition['type'], 'entity_revision:') === 0) {
      $entity_type_id = substr($definition['type'], strlen('entity:'));
      if (strpos($definition['type'], '{') !== FALSE) {
        $entity_type_slug = substr($entity_type_id, 1, -1);
        return $name != $entity_type_slug && in_array($entity_type_slug, $route
          ->compile()
          ->getVariables(), TRUE);
      }
      return $this->entityTypeManager
        ->hasDefinition($entity_type_id);
    }
    return FALSE;
  }

  /**
   * Determines the entity type ID given a route definition and route defaults.
   *
   * @param mixed $definition
   *   The parameter definition provided in the route options.
   * @param string $name
   *   The name of the parameter.
   * @param array $defaults
   *   The route defaults array.
   *
   * @throws \Drupal\Core\ParamConverter\ParamNotConvertedException
   *   Thrown when the dynamic entity type is not found in the route defaults.
   *
   * @return string
   *   The entity type ID.
   */
  protected function getEntityTypeFromDefaults($definition, $name, array $defaults) {
    $entity_type_id = substr($definition['type'], strlen('entity_revision:'));

    // If the entity type is dynamic, it will be pulled from the route defaults.
    if (strpos($entity_type_id, '{') === 0) {
      $entity_type_slug = substr($entity_type_id, 1, -1);
      if (!isset($defaults[$entity_type_slug])) {
        throw new ParamNotConvertedException(sprintf('The "%s" parameter was not converted because the "%s" parameter is missing', $name, $entity_type_slug));
      }
      $entity_type_id = $defaults[$entity_type_slug];
    }
    return $entity_type_id;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityRevisionConverter::$entityRepository protected property The entity repository.
EntityRevisionConverter::$entityTypeManager protected property The entity type manager.
EntityRevisionConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides ParamConverterInterface::applies
EntityRevisionConverter::convert public function Converts path variables to their corresponding objects. Overrides ParamConverterInterface::convert
EntityRevisionConverter::getEntityTypeFromDefaults protected function Determines the entity type ID given a route definition and route defaults.
EntityRevisionConverter::__construct public function Constructs a new EntityRevisionConverter.