You are here

public function EntityRevisionConverter::convert in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/ParamConverter/EntityRevisionConverter.php \Drupal\workbench_moderation\ParamConverter\EntityRevisionConverter::convert()

Converts path variables to their corresponding objects.

Parameters

mixed $value: The raw value.

mixed $definition: The parameter definition provided in the route options.

string $name: The name of the parameter.

array $defaults: The route defaults array.

Return value

mixed|null The converted parameter value.

Overrides EntityConverter::convert

File

src/ParamConverter/EntityRevisionConverter.php, line 88

Class

EntityRevisionConverter
Defines a class for making sure the edit-route loads the current draft.

Namespace

Drupal\workbench_moderation\ParamConverter

Code

public function convert($value, $definition, $name, array $defaults) {
  $entity = parent::convert($value, $definition, $name, $defaults);
  if ($entity && $this->moderationInformation
    ->isModeratableEntity($entity) && !$this->moderationInformation
    ->isLatestRevision($entity)) {
    $entity_type_id = $this
      ->getEntityTypeFromDefaults($definition, $name, $defaults);
    $entity = $this->moderationInformation
      ->getLatestRevision($entity_type_id, $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;
}