LingotekContentModerationHandler.php in Lingotek Translation 3.8.x
File
src/Moderation/LingotekContentModerationHandler.php
View source
<?php
namespace Drupal\lingotek\Moderation;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\workflows\WorkflowInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LingotekContentModerationHandler implements LingotekModerationHandlerInterface {
use LingotekContentModerationCheckTrait;
protected $entityTypeManager;
protected $moderationConfiguration;
protected $entityTypeBundleInfo;
protected $moderationInfo;
public function __construct(ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, LingotekModerationConfigurationServiceInterface $moderation_configuration, EntityTypeBundleInfoInterface $entity_type_bundle_info, ContainerInterface $container) {
$this
->setModuleHandler($module_handler);
$this->entityTypeManager = $entity_type_manager;
$this->moderationConfiguration = $moderation_configuration;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
if ($container
->has('content_moderation.moderation_information')) {
$this->moderationInfo = $container
->get('content_moderation.moderation_information');
}
}
public function shouldModerationPreventUpload(EntityInterface $entity) {
$prevent = FALSE;
if ($this->moduleHandler
->moduleExists('content_moderation')) {
$moderationEnabled = $this
->isModerationEnabled($entity);
if ($moderationEnabled) {
$uploadStatus = $this->moderationConfiguration
->getUploadStatus($entity
->getEntityTypeId(), $entity
->bundle());
$state = $this
->getModerationState($entity);
if (!empty($state) && $state !== $uploadStatus) {
$prevent = TRUE;
}
}
}
return $prevent;
}
public function performModerationTransitionIfNeeded(ContentEntityInterface &$entity) {
if ($this->moderationInfo
->shouldModerateEntitiesOfBundle($entity
->getEntityType(), $entity
->bundle())) {
$transition = $this->moderationConfiguration
->getDownloadTransition($entity
->getEntityTypeId(), $entity
->bundle());
if ($transition) {
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity
->getEntityTypeId());
$workflow = NULL;
if (isset($bundles[$entity
->bundle()]['workflow'])) {
$workflow = $this->entityTypeManager
->getStorage('workflow')
->load($bundles[$entity
->bundle()]['workflow']);
if ($workflow && $workflow
->getTypePlugin()
->hasTransition($transition)) {
$theTransition = $workflow
->getTypePlugin()
->getTransition($transition);
if ($theTransition !== NULL) {
$state = $this
->getModerationState($entity);
$validStates = $theTransition
->from();
$validStatesIds = array_keys($validStates);
if (in_array($state, $validStatesIds)) {
$this
->setModerationState($entity, $theTransition
->to()
->id());
}
}
}
else {
\Drupal::logger('lingotek')
->warning('Cannot execute transition for @bundle @entity as the workflow @workflow transition @transition cannot be loaded', [
'@bundle' => $entity
->bundle(),
'@entity' => (string) $entity
->label(),
'@workflow' => (string) $workflow
->label(),
'@transition' => $transition,
]);
}
}
}
}
}
protected function getWorkflowTransition(WorkflowInterface $workflow, $transition_id) {
return $workflow
->getTypePlugin()
->getTransition($transition_id);
}
public function getModerationState(ContentEntityInterface $entity) {
$state = NULL;
if ($this->moderationInfo
->isModeratedEntity($entity)) {
$state = $entity
->get('moderation_state')
->getString();
}
return $state;
}
public function setModerationState(ContentEntityInterface $entity, $state) {
$entity
->set('moderation_state', $state);
}
public function isModerationEnabled(EntityInterface $entity) {
$moderationEnabled = FALSE;
$moderationClass = $entity
->getEntityType()
->getHandlerClass('moderation');
if ($moderationClass) {
$implements = class_implements($moderationClass);
$moderationEnabled = in_array('Drupal\\content_moderation\\Entity\\Handler\\ModerationHandlerInterface', $implements);
}
return $moderationEnabled;
}
}