EntityRevisionConverter.php in Workbench Moderation 8
File
src/ParamConverter/EntityRevisionConverter.php
View source
<?php
namespace Drupal\workbench_moderation\ParamConverter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\ParamConverter\EntityConverter;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\workbench_moderation\ModerationInformationInterface;
use Symfony\Component\Routing\Route;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class EntityRevisionConverter extends EntityConverter {
protected $moderationInformation;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_info, EntityRepositoryInterface $entity_repository) {
parent::__construct($entity_type_manager, $entity_repository);
$this->moderationInformation = $moderation_info;
}
public function applies($definition, $name, Route $route) {
return $this
->hasForwardRevisionFlag($definition) || $this
->isEditFormPage($route);
}
protected function hasForwardRevisionFlag(array $definition) {
return isset($definition['load_forward_revision']) && $definition['load_forward_revision'];
}
protected function isEditFormPage(Route $route) {
if ($default = $route
->getDefault('_entity_form')) {
$default .= '.default';
[
$entity_type_id,
$operation,
] = explode('.', $default);
if (!$this->entityTypeManager
->hasDefinition($entity_type_id)) {
return FALSE;
}
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
return $operation == 'edit' && $entity_type && $entity_type
->isRevisionable();
}
}
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 ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
$entity = $this->entityRepository
->getTranslationFromContext($entity, NULL, [
'operation' => 'entity_upcast',
]);
}
}
return $entity;
}
}