You are here

class LingotekWorkbenchModerationHandler in Lingotek Translation 3.4.x

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

Workbench moderation handler managing the Lingotek integration.

@package Drupal\lingotek\Moderation

Hierarchy

Expanded class hierarchy of LingotekWorkbenchModerationHandler

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

File

src/Moderation/LingotekWorkbenchModerationHandler.php, line 16

Namespace

Drupal\lingotek\Moderation
View source
class LingotekWorkbenchModerationHandler implements LingotekModerationHandlerInterface {
  use LingotekWorkbenchModerationCheckTrait;

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

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

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

  /**
   * Constructs a new LingotekWorkbenchModerationHandler 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 \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, ContainerInterface $container) {
    $this
      ->setModuleHandler($module_handler);
    $this->entityTypeManager = $entity_type_manager;
    $this->moderationConfiguration = $moderation_configuration;

    // 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('workbench_moderation.moderation_information')) {
      $this->moderationInfo = $container
        ->get('workbench_moderation.moderation_information');
    }
  }

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

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

          // Ensure we can execute this transition.
          if ($this
            ->getModerationState($entity) === $transition
            ->getFromState()) {
            $this
              ->setModerationState($entity, $transition
              ->getToState());
          }
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getModerationState(ContentEntityInterface $entity) {
    return $entity
      ->get('moderation_state')->target_id;
  }

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

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

}

Members

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