Commit c8fd13fa authored by Gaurav Agrawal's avatar Gaurav Agrawal Committed by Martin Kolar
Browse files

Issue #3210595: Update processors to support all available moderated entities

parent 0a6b9d5b
Loading
Loading
Loading
Loading
+56 −9
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\search_api_revisions\Plugin\search_api\processor;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Datasource\DatasourceInterface;
@@ -31,17 +33,37 @@ class ContentModerationLastOfState extends ProcessorPluginBase {
   */
  protected $moderationInformation;

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

  /**
   * Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $plugin->setEntityTypeManager($container->get('entity_type.manager'));
    $plugin->setModuleHandler($container->get('module_handler'));
    $plugin->setModerationInformation($container);

    return $plugin;
  }

  /**
   * Helper function to check content_moderation is enabled.
   */
  public function isHidden() {
    if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
    if ($this->moduleHandler->moduleExists('content_moderation')) {
      return FALSE;
    }
    return TRUE;
@@ -54,11 +76,31 @@ class ContentModerationLastOfState extends ProcessorPluginBase {
   *   Moderation information.
   */
  protected function setModerationInformation(ContainerInterface $container) {
    if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
    if ($this->moduleHandler->moduleExists('content_moderation')) {
      $this->moderationInformation = $container->get('content_moderation.moderation_information');
    }
  }

  /**
   * Sets the entity type manager service.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Set the module handler service.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler service.
   */
  protected function setModuleHandler(ModuleHandlerInterface $moduleHandler) {
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * {@inheritdoc}
   */
@@ -67,7 +109,10 @@ class ContentModerationLastOfState extends ProcessorPluginBase {
    if (!($datasource instanceof ContentEntity)) {
      return $properties;
    }
    if ($datasource->getEntityTypeId() == 'node') {

    $entity_type = $this->entityTypeManager->getDefinition($datasource->getEntityTypeId());

    if ($this->moderationInformation->isModeratedEntityType($entity_type)) {
      $definition = [
        'label' => $this->t('Content moderation - last of state'),
        'description' => "Checks whether this revision is latest in its state.",
@@ -88,15 +133,16 @@ class ContentModerationLastOfState extends ProcessorPluginBase {
    if (!($object instanceof ComplexDataInterface)) {
      return;
    }
    $entity = $object->getEntity();
    $fields = $this->getFieldsHelper()
      ->filterForPropertyPath($item->getFields(), 'entity:node', 'content_moderation_last_of_state');
      ->filterForPropertyPath($item->getFields(), 'entity:' . $entity->getEntityTypeId(), 'content_moderation_last_of_state');
    if (empty($fields)) {
      $fields = $this->getFieldsHelper()
        ->filterForPropertyPath($item->getFields(), 'entity_revision:node', 'content_moderation_last_of_state');
        ->filterForPropertyPath($item->getFields(), 'entity_revision:' . $entity->getEntityTypeId(), 'content_moderation_last_of_state');
    }

    foreach ($fields as $field) {
      if (in_array($field->getDatasourceId(), ['entity:node', 'entity_revision:node'])) {
      if (in_array($field->getDatasourceId(), ['entity:' . $entity->getEntityTypeId(), 'entity_revision:' . $entity->getEntityTypeId()])) {

        /** @var \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList $moderation_state */
        $moderation_state = $object->get('moderation_state');
@@ -105,13 +151,14 @@ class ContentModerationLastOfState extends ProcessorPluginBase {
        $entity = $object->getValue();

        // @TODO: use drupal_static here?
        $cms_revisions = \Drupal::entityQuery('content_moderation_state')
        $cms_revisions = $this->entityTypeManager->getStorage('content_moderation_state')->getQuery()
          ->allRevisions()
          ->condition('content_entity_id', $entity->id())
          ->condition('content_entity_type_id', $entity->getEntityTypeId())
          ->condition('moderation_state', $moderation_state->get(0)->getValue()['value'])
          ->execute();

        $cms_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');
        $cms_storage = $this->entityTypeManager->getStorage('content_moderation_state');
        $entity_revisions_ids = [];
        foreach (array_keys($cms_revisions) as $cms_revision_id) {
          /** @var \Drupal\content_moderation\Entity\ContentModerationState $cms_revision */
@@ -119,7 +166,7 @@ class ContentModerationLastOfState extends ProcessorPluginBase {
          $entity_revisions_ids[] = $cms_revision->content_entity_revision_id->value;
        }

        if ($entity->getLoadedRevisionId() == max($entity_revisions_ids)) {
        if (empty($entity_revisions_ids) || $entity->getLoadedRevisionId() == max($entity_revisions_ids)) {
          $field->addValue(TRUE);
        }
        else {
+51 −6
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\search_api_revisions\Plugin\search_api\processor;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Datasource\DatasourceInterface;
@@ -31,17 +33,37 @@ class ContentModerationState extends ProcessorPluginBase {
   */
  protected $moderationInformation;

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

  /**
   * Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $plugin->setEntityTypeManager($container->get('entity_type.manager'));
    $plugin->setModuleHandler($container->get('module_handler'));
    $plugin->setModerationInformation($container);

    return $plugin;
  }

  /**
   * Helper function to check content_moderation is enabled.
   */
  public function isHidden() {
    if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
    if ($this->moduleHandler->moduleExists('content_moderation')) {
      return FALSE;
    }
    return TRUE;
@@ -54,11 +76,31 @@ class ContentModerationState extends ProcessorPluginBase {
   *   Moderation information.
   */
  protected function setModerationInformation(ContainerInterface $container) {
    if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
    if ($this->moduleHandler->moduleExists('content_moderation')) {
      $this->moderationInformation = $container->get('content_moderation.moderation_information');
    }
  }

  /**
   * Sets the entity type manager service.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Set the module handler service.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler service.
   */
  protected function setModuleHandler(ModuleHandlerInterface $moduleHandler) {
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * {@inheritdoc}
   */
@@ -67,7 +109,9 @@ class ContentModerationState extends ProcessorPluginBase {
    if (!($datasource instanceof ContentEntity)) {
      return $properties;
    }
    if ($datasource->getEntityTypeId() == 'node') {
    $entity_type = $this->entityTypeManager->getDefinition($datasource->getEntityTypeId());

    if ($this->moderationInformation->isModeratedEntityType($entity_type)) {
      $definition = [
        'label' => $this->t('Content moderation state'),
        'description' => "Moderation state of current node revision.",
@@ -88,17 +132,18 @@ class ContentModerationState extends ProcessorPluginBase {
    if (!($object instanceof ComplexDataInterface)) {
      return;
    }
    $entity = $object->getEntity();
    $fields = $this->getFieldsHelper()
      ->filterForPropertyPath($item->getFields(), 'entity:node', 'content_moderation_state');
      ->filterForPropertyPath($item->getFields(), 'entity:' . $entity->getEntityTypeId(), 'content_moderation_state');
    if (empty($fields)) {
      $fields = $this->getFieldsHelper()
        ->filterForPropertyPath($item->getFields(), 'entity_revision:node', 'content_moderation_state');
        ->filterForPropertyPath($item->getFields(), 'entity_revision:' . $entity->getEntityTypeId(), 'content_moderation_state');
    }

    /** @var \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList $moderation_state */
    $moderation_state = $object->get('moderation_state');
    foreach ($fields as $field) {
      if (in_array($field->getDatasourceId(), ['entity:node', 'entity_revision:node'])) {
      if (in_array($field->getDatasourceId(), ['entity:' . $entity->getEntityTypeId(), 'entity_revision:' . $entity->getEntityTypeId()])) {
        $field->addValue($moderation_state->get(0)->getValue()['value']);
      }
    }