Commit 4b342748 authored by Ruturaj Chaubey's avatar Ruturaj Chaubey Committed by Dieter Holvoet
Browse files

Issue #3271149 by Ruturaj Chaubey, DieterHolvoet: The 'User Widget' dashboard...

Issue #3271149 by Ruturaj Chaubey, DieterHolvoet: The 'User Widget' dashboard block has an implicit dependency on content_kanban
parent d5214fa6
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -10,3 +10,7 @@ services:
  content_planner.dashboard_block_plugin_manager:
    class: Drupal\content_planner\DashboardBlockPluginManager
    parent: default_plugin_manager

  content_planner.content_moderation_service:
    class: Drupal\content_planner\ContentModerationService
    arguments: ['@entity_type.manager']
+8 −8
Original line number Diff line number Diff line
@@ -4,13 +4,13 @@ namespace Drupal\content_kanban\Form;

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\content_kanban\KanbanService;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\content_kanban\Form\SettingsForm;
use Drupal\content_planner\ContentModerationService;
use Drupal\node\Entity\NodeType;

/**
@@ -19,11 +19,11 @@ use Drupal\node\Entity\NodeType;
class KanbanFilterForm extends FormBase {

  /**
   * The Kanban service.
   * The Content Moderation service.
   *
   * @var \Drupal\content_kanban\KanbanService
   * @var \Drupal\content_planner\ContentModerationService
   */
  protected $kanbanService;
  protected $contentModerationService;

  /**
   * An array with the form params.
@@ -42,8 +42,8 @@ class KanbanFilterForm extends FormBase {
  /**
   * {@inheritdoc}
   */
  public function __construct(KanbanService $kanban_service, EntityTypeManager $entityTypeManager) {
    $this->kanbanService = $kanban_service;
  public function __construct(ContentModerationService $content_moderation_service, EntityTypeManager $entityTypeManager) {
    $this->contentModerationService = $content_moderation_service;
    $this->entityTypeManager = $entityTypeManager;
  }

@@ -52,7 +52,7 @@ class KanbanFilterForm extends FormBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('content_kanban.kanban_service'),
      $container->get('content_planner.content_moderation_service'),
      $container->get('entity_type.manager')
    );
  }
@@ -165,7 +165,7 @@ class KanbanFilterForm extends FormBase {
    $options = [];

    // Load Content Moderation entities.
    $content_moderation_entities = $this->kanbanService->getEntityContentModerationEntities($this->formParams['workflow_id']);
    $content_moderation_entities = $this->contentModerationService->getEntityContentModerationEntities($this->formParams['workflow_id']);
    foreach ($content_moderation_entities as $content_moderation_entity) {
      // Get the entity id and entity type id.
      $entityId = $content_moderation_entity->content_entity_id->value;
+0 −84
Original line number Diff line number Diff line
@@ -195,90 +195,6 @@ class KanbanService {
    return $this->definedColors[$index];
  }

  /**
   * Gets the Content Moderation Entities.
   *
   * @param string $workflow
   *   The workflow ID.
   * @param array $filters
   *   An array with the filters.
   * @param array $entities
   *   An array with the entities.
   *
   * @return \Drupal\content_moderation\Entity\ContentModerationState[]
   *   Returns an array with the content moderation states for the given
   *   workflow.
   */
  public function getEntityContentModerationEntities($workflow, array $filters = [], array $entities = []) {
    $result = [];
    try {
      $query = $this->entityTypeManager->getStorage('content_moderation_state')->getQuery();
      if (!empty(array_keys($entities))) {
        $query->condition('workflow', $workflow);
        $query->condition('content_entity_type_id', array_keys($entities), 'in');
      }

      // Moderation state filter.
      if (array_key_exists('moderation_state', $filters) && $filters['moderation_state']) {
        $query->condition('moderation_state', $filters['moderation_state']);
      }

      // User ID filter.
      if (array_key_exists('uid', $filters) && $filters['uid']) {
        $query->condition('uid', $filters['uid']);
      }

      // User ID filter.
      $result = $query->execute();
    }
    catch (InvalidPluginDefinitionException $e) {
      watchdog_exception('content_kanban', $e);
    }
    catch (PluginNotFoundException $e) {
      watchdog_exception('content_kanban', $e);
    }
    if ($result) {
      return ContentModerationState::loadMultiple($result);
    }

    return $result;
  }

  /**
   * Gets the entity IDs from Content Moderation entities.
   *
   * @param string $workflow
   *   The workflow id.
   * @param array $filters
   *   An array with the filters.
   * @param array $entities
   *   An array with the entities.
   *
   * @return array
   *   Returns an array with the entity ids.
   */
  public function getEntityIdsFromContentModerationEntities($workflow, array $filters = [], array $entities = []) {
    $entityIds = [];

    if ($content_moderation_states = $this->getEntityContentModerationEntities($workflow, $filters, $entities)) {
      foreach ($content_moderation_states as $content_moderation_state) {

        // Get property.
        $content_entity_id_property = $content_moderation_state->content_entity_id;

        // Get value.
        $content_entity_id_value = $content_entity_id_property->getValue();
        $entity_type_id_value = $content_moderation_state->get('content_entity_type_id')->getValue();
        // Get the entity type id.
        $entity_type_id = $entity_type_id_value[0]['value'];
        // Build the ids array with entity type as key.
        $entityIds[$entity_type_id][] = $content_entity_id_value[0]['value'];
      }

    }
    return $entityIds;
  }

  /**
   * Gets the entities by Type.
   *
+116 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\content_planner;

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\Core\Entity\EntityTypeManagerInterface;


/**
 * Class ContentModerationService.
 */
class ContentModerationService {

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

  /**
   * Constructs a new ContentModerationService object.
   */
  public function __construct(
    EntityTypeManagerInterface $entityTypeManager
  ) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Gets the Content Moderation Entities.
   *
   * @param string $workflow
   *   The workflow ID.
   * @param array $filters
   *   An array with the filters.
   * @param array $entities
   *   An array with the entities.
   *
   * @return \Drupal\content_moderation\Entity\ContentModerationState[]
   *   Returns an array with the content moderation states for the given
   *   workflow.
   */
  public function getEntityContentModerationEntities($workflow, array $filters = [], array $entities = []) {
    $result = [];
    try {
      $query = $this->entityTypeManager->getStorage('content_moderation_state')->getQuery();
      if (!empty(array_keys($entities))) {
        $query->condition('workflow', $workflow);
        $query->condition('content_entity_type_id', array_keys($entities), 'in');
      }

      // Moderation state filter.
      if (array_key_exists('moderation_state', $filters) && $filters['moderation_state']) {
        $query->condition('moderation_state', $filters['moderation_state']);
      }

      // User ID filter.
      if (array_key_exists('uid', $filters) && $filters['uid']) {
        $query->condition('uid', $filters['uid']);
      }

      // User ID filter.
      $result = $query->execute();
    }
    catch (InvalidPluginDefinitionException $e) {
      watchdog_exception('content_planner', $e);
    }
    catch (PluginNotFoundException $e) {
      watchdog_exception('content_planner', $e);
    }
    if ($result) {
      return ContentModerationState::loadMultiple($result);
    }

    return $result;
  }

  /**
   * Gets the entity IDs from Content Moderation entities.
   *
   * @param string $workflow
   *   The workflow id.
   * @param array $filters
   *   An array with the filters.
   * @param array $entities
   *   An array with the entities.
   *
   * @return array
   *   Returns an array with the entity ids.
   */
  public function getEntityIdsFromContentModerationEntities($workflow, array $filters = [], array $entities = []) {
    $entityIds = [];

    if ($content_moderation_states = $this->getEntityContentModerationEntities($workflow, $filters, $entities)) {
      foreach ($content_moderation_states as $content_moderation_state) {

        // Get property.
        $content_entity_id_property = $content_moderation_state->content_entity_id;

        // Get value.
        $content_entity_id_value = $content_entity_id_property->getValue();
        $entity_type_id_value = $content_moderation_state->get('content_entity_type_id')->getValue();
        // Get the entity type id.
        $entity_type_id = $entity_type_id_value[0]['value'];
        // Build the ids array with entity type as key.
        $entityIds[$entity_type_id][] = $content_entity_id_value[0]['value'];
      }

    }
    return $entityIds;
  }

}
+2 −2
Original line number Diff line number Diff line
@@ -139,13 +139,13 @@ class UserBlock extends DashboardBlockBase {
   *   The content count for the given user and the given moderation state.
   */
  public function getUserContentWorkflowCount($user_id, $moderation_state) {
    $kanban_service = \Drupal::service('content_kanban.kanban_service');
    $content_moderation_service = \Drupal::service('content_planner.content_moderation_service');

    $filters = [
      'uid' => $user_id,
      'moderation_state' => $moderation_state,
    ];
    $nids = $kanban_service->getEntityIdsFromContentModerationEntities('netnode', $filters);
    $nids = $content_moderation_service->getEntityIdsFromContentModerationEntities('netnode', $filters);

    return count($nids);
  }