You are here

class LingotekContentModerationHandler in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8.2 src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  2. 4.0.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  3. 3.0.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  4. 3.1.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  5. 3.2.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  6. 3.3.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  7. 3.5.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  8. 3.6.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  9. 3.7.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler
  10. 3.8.x src/Moderation/LingotekContentModerationHandler.php \Drupal\lingotek\Moderation\LingotekContentModerationHandler

Content moderation handler managing the Lingotek integration.

@package Drupal\lingotek\Moderation

Hierarchy

Expanded class hierarchy of LingotekContentModerationHandler

1 string reference to 'LingotekContentModerationHandler'
lingotek.services.yml in ./lingotek.services.yml
lingotek.services.yml
1 service uses LingotekContentModerationHandler
lingotek.content_moderation_handler in ./lingotek.services.yml
Drupal\lingotek\Moderation\LingotekContentModerationHandler

File

src/Moderation/LingotekContentModerationHandler.php, line 18

Namespace

Drupal\lingotek\Moderation
View source
class LingotekContentModerationHandler implements LingotekModerationHandlerInterface {
  use LingotekContentModerationCheckTrait;

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

  /**
   * The moderation configuration.
   *
   * @var \Drupal\lingotek\Moderation\LingotekModerationConfigurationServiceInterface
   */
  protected $moderationConfiguration;

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The moderation information service.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface
   */
  protected $moderationInfo;

  /**
   * Constructs a new LingotekContentModerationHandler object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\lingotek\Moderation\LingotekModerationConfigurationServiceInterface $moderation_configuration
   *   A Lingotek moderation configuration service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The container from which optional services can be requested.
   */
  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;

    // We need a service we cannot depend on, as it may not exist if the module
    // is not present. Ignore the error.
    if ($container
      ->has('content_moderation.moderation_information')) {
      $this->moderationInfo = $container
        ->get('content_moderation.moderation_information');
    }
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'])) {

          /** @var \Drupal\workflows\WorkflowInterface $workflow */
          $workflow = $this->entityTypeManager
            ->getStorage('workflow')
            ->load($bundles[$entity
            ->bundle()]['workflow']);
          if ($workflow) {
            $theTransition = $workflow
              ->getTypePlugin()
              ->getTransition($transition);
            if ($theTransition !== NULL) {

              // Ensure we can execute this transition.
              $state = $this
                ->getModerationState($entity);
              $validStates = $theTransition
                ->from();
              $validStatesIds = array_keys($validStates);
              if (in_array($state, $validStatesIds)) {
                $this
                  ->setModerationState($entity, $theTransition
                  ->to()
                  ->id());
              }
            }
          }
        }
      }
    }
  }

  /**
   * Get workflow transition helper method.
   *
   * @param \Drupal\workflows\WorkflowInterface $workflow
   *   The workflow.
   * @param $transition_id
   *   The transition id.
   *
   * @return \Drupal\workflows\TransitionInterface
   *   A transition.
   *
   * @deprecated in lingotek:3.0.0 and is removed from lingotek:4.0.0.
   *   Use $workflow->getTypePlugin()->getTransition($transition_id) instead.
   * @see \Drupal\workflows\WorkflowTypeInterface::getTransition()
   */
  protected function getWorkflowTransition(WorkflowInterface $workflow, $transition_id) {
    return $workflow
      ->getTypePlugin()
      ->getTransition($transition_id);
  }

  /**
   * {@inheritdoc}
   */
  public function getModerationState(ContentEntityInterface $entity) {
    $state = NULL;
    if ($this->moderationInfo
      ->isModeratedEntity($entity)) {
      $state = $entity
        ->get('moderation_state')
        ->getString();
    }
    return $state;
  }

  /**
   * {@inheritdoc}
   */
  public function setModerationState(ContentEntityInterface $entity, $state) {
    $entity
      ->set('moderation_state', $state);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekContentModerationCheckTrait::$moduleHandler protected property The module handler service.
LingotekContentModerationCheckTrait::applies public function
LingotekContentModerationCheckTrait::setModuleHandler public function
LingotekContentModerationHandler::$entityTypeBundleInfo protected property The entity type bundle info service.
LingotekContentModerationHandler::$entityTypeManager protected property The entity type manager.
LingotekContentModerationHandler::$moderationConfiguration protected property The moderation configuration.
LingotekContentModerationHandler::$moderationInfo protected property The moderation information service.
LingotekContentModerationHandler::getModerationState public function Gets the moderation state ID. Overrides LingotekModerationHandlerInterface::getModerationState
LingotekContentModerationHandler::getWorkflowTransition Deprecated protected function Get workflow transition helper method.
LingotekContentModerationHandler::isModerationEnabled public function Checks if the moderation is enabled for this entity. Overrides LingotekModerationHandlerInterface::isModerationEnabled
LingotekContentModerationHandler::performModerationTransitionIfNeeded public function Performs a moderation transition if needed. Overrides LingotekModerationHandlerInterface::performModerationTransitionIfNeeded
LingotekContentModerationHandler::setModerationState public function Sets the moderation state ID. Overrides LingotekModerationHandlerInterface::setModerationState
LingotekContentModerationHandler::shouldModerationPreventUpload public function Checks if we should prevent upload based on content moderation settings. Overrides LingotekModerationHandlerInterface::shouldModerationPreventUpload
LingotekContentModerationHandler::__construct public function Constructs a new LingotekContentModerationHandler object.