LingotekWorkbenchModerationHandler.php in Lingotek Translation 3.1.x
File
src/Moderation/LingotekWorkbenchModerationHandler.php
View source
<?php
namespace Drupal\lingotek\Moderation;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LingotekWorkbenchModerationHandler implements LingotekModerationHandlerInterface {
use LingotekWorkbenchModerationCheckTrait;
protected $entityTypeManager;
protected $moderationConfiguration;
protected $moderationInfo;
public function __construct(ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, LingotekModerationConfigurationServiceInterface $moderation_configuration, ContainerInterface $container) {
$this
->setModuleHandler($module_handler);
$this->entityTypeManager = $entity_type_manager;
$this->moderationConfiguration = $moderation_configuration;
if ($container
->has('workbench_moderation.moderation_information')) {
$this->moderationInfo = $container
->get('workbench_moderation.moderation_information');
}
}
public function shouldModerationPreventUpload(EntityInterface $entity) {
$prevent = FALSE;
if ($this->moduleHandler
->moduleExists('workbench_moderation')) {
$moderationEnabled = $this
->isModerationEnabled($entity);
if ($moderationEnabled) {
$uploadStatus = $this->moderationConfiguration
->getUploadStatus($entity
->getEntityTypeId(), $entity
->bundle());
$state = $this
->getModerationState($entity);
if ($state !== $uploadStatus) {
$prevent = TRUE;
}
}
}
return $prevent;
}
public function performModerationTransitionIfNeeded(ContentEntityInterface &$entity) {
if ($this->moderationInfo
->isModeratableBundle($entity
->getEntityType(), $entity
->bundle())) {
$transition = $this->moderationConfiguration
->getDownloadTransition($entity
->getEntityTypeId(), $entity
->bundle());
if ($transition !== NULL) {
$transition = $this->entityTypeManager
->getStorage('moderation_state_transition')
->load($transition);
if ($transition !== NULL) {
if ($this
->getModerationState($entity) === $transition
->getFromState()) {
$this
->setModerationState($entity, $transition
->getToState());
}
}
}
}
}
public function getModerationState(ContentEntityInterface $entity) {
return $entity
->get('moderation_state')->target_id;
}
public function setModerationState(ContentEntityInterface $entity, $state) {
$entity
->set('moderation_state', $state);
}
public function isModerationEnabled(EntityInterface $entity) {
$moderationEnabled = FALSE;
$entityType = $entity
->getEntityType();
$bundleEntityType = $entityType
->getBundleEntityType();
if ($bundleEntityType !== NULL) {
$bundleType = $this->entityTypeManager
->getStorage($bundleEntityType)
->load($entity
->bundle());
$moderationEnabled = $bundleType
->getThirdPartySetting('workbench_moderation', 'enabled', FALSE);
}
return $moderationEnabled;
}
}