You are here

class KanbanWorkflowService in Content Planner 8

Class KanbanWorkflowService.

Hierarchy

Expanded class hierarchy of KanbanWorkflowService

2 files declare their use of KanbanWorkflowService
KanbanController.php in modules/content_kanban/src/Controller/KanbanController.php
RecentKanbanActivities.php in modules/content_kanban/src/Plugin/DashboardBlock/RecentKanbanActivities.php
1 string reference to 'KanbanWorkflowService'
content_kanban.services.yml in modules/content_kanban/content_kanban.services.yml
modules/content_kanban/content_kanban.services.yml
1 service uses KanbanWorkflowService
content_kanban.kanban_workflow_service in modules/content_kanban/content_kanban.services.yml
Drupal\content_kanban\KanbanWorkflowService

File

modules/content_kanban/src/KanbanWorkflowService.php, line 15

Namespace

Drupal\content_kanban
View source
class KanbanWorkflowService {
  use StringTranslationTrait;

  /**
   * The database connection service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

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

  /**
   * The Kanban Log service.
   *
   * @var \Drupal\content_kanban\KanbanLogService
   */
  protected $kanbanLogService;

  /**
   * Constructs a new NewsService object.
   */
  public function __construct(Connection $database, ModerationInformationInterface $moderation_information, KanbanLogService $kanban_log_service) {
    $this->database = $database;
    $this->moderationInformation = $moderation_information;
    $this->kanbanLogService = $kanban_log_service;
  }

  /**
   * Acts upon a entity presave.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The current entity that is saved.
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The user that is related to the entity save.
   *
   * @see content_kanban_entity_presave()
   */
  public function onEntityPresave(ContentEntityInterface $entity, AccountInterface $user) {

    // If the entity is moderated, meaning it belongs to a certain workflow.
    if ($this->moderationInformation
      ->isModeratedEntity($entity)) {
      $current_state = $this
        ->getCurrentStateId($entity);
      $prev_state = $this
        ->getPreviousWorkflowStateId($entity);
      if ($current_state && $prev_state) {

        // Generate name for entity.
        $name = $this
          ->t('Workflow State change on Entity')
          ->render();

        // Get workflow from moderated entity.
        $workflow = $this->moderationInformation
          ->getWorkflowForEntity($entity);

        // Create new log entity.
        $this->kanbanLogService
          ->createLogEntity($name, $user
          ->id(), $entity
          ->id(), $entity
          ->getEntityTypeId(), $workflow
          ->id(), $prev_state, $current_state);
      }
    }
  }

  /**
   * Gets the current State ID.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity to check.
   *
   * @return mixed
   *   Returns the current moderation state id for the given entity.
   */
  public function getCurrentStateId(ContentEntityInterface $entity) {
    return $entity->moderation_state->value;
  }

  /**
   * Gets the label of the current state of a given entity.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity object.
   *
   * @return bool|string
   *   Returns the current state if any, FALSE otherwise.
   */
  public function getCurrentStateLabel(ContentEntityInterface $entity) {
    if ($this->moderationInformation
      ->isModeratedEntity($entity)) {
      if ($workflow = $this->moderationInformation
        ->getWorkflowForEntity($entity)) {
        if ($states = self::getWorkflowStates($workflow)) {
          $entity_workflow_state = $this
            ->getCurrentStateId($entity);
          if (array_key_exists($entity_workflow_state, $states)) {
            return $states[$entity_workflow_state];
          }
        }
      }
    }
    return FALSE;
  }

  /**
   * Get Workflow States.
   *
   * @param \Drupal\workflows\Entity\Workflow $workflow
   *   The workflow object.
   *
   * @return array
   *   Returns an array with the available workflow states.
   */
  public static function getWorkflowStates(Workflow $workflow) {
    $states = [];
    $type_settings = $workflow
      ->get('type_settings');

    // Sort by weight.
    uasort($type_settings['states'], function ($a, $b) {
      if ($a['weight'] == $b['weight']) {
        return 0;
      }
      elseif ($a['weight'] < $b['weight']) {
        return -1;
      }
      else {
        return 1;
      }
    });
    foreach ($type_settings['states'] as $state_id => $state) {
      $states[$state_id] = $state['label'];
    }
    return $states;
  }

  /**
   * Get ID of the previous workflow state.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity object.
   *
   * @return string
   *   Returns the previous state id.
   */
  public function getPreviousWorkflowStateId(ContentEntityInterface $entity) {
    $workflow = $this->moderationInformation
      ->getWorkflowForEntity($entity);
    if ($state_history = $this
      ->getWorkflowStateHistory($workflow
      ->id(), $entity)) {
      if (isset($state_history[0])) {
        return $state_history[0];
      }
    }
    $state = $workflow
      ->getTypePlugin()
      ->getInitialState($entity);
    return $state
      ->id();
  }

  /**
   * Gets the workflow state history of a given entity.
   *
   * @param string $workflow_id
   *   A string representing the workflow id.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity for which the workflow history is requested.
   *
   * @return array
   *   An array with the workflow state history for the given entity.
   */
  public function getWorkflowStateHistory($workflow_id, ContentEntityInterface $entity) {
    $query = $this->database
      ->select('content_moderation_state_field_revision', 'r');
    $query
      ->addField('r', 'moderation_state');
    $query
      ->condition('r.workflow', $workflow_id);
    $query
      ->condition('r.content_entity_type_id', $entity
      ->getEntityTypeId());
    $query
      ->condition('r.content_entity_id', $entity
      ->id());
    $query
      ->orderBy('r.revision_id', 'DESC');
    $result = $query
      ->execute()
      ->fetchAll();
    $return = [];
    foreach ($result as $row) {
      $return[] = $row->moderation_state;
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KanbanWorkflowService::$database protected property The database connection service.
KanbanWorkflowService::$kanbanLogService protected property The Kanban Log service.
KanbanWorkflowService::$moderationInformation protected property The moderation information service.
KanbanWorkflowService::getCurrentStateId public function Gets the current State ID.
KanbanWorkflowService::getCurrentStateLabel public function Gets the label of the current state of a given entity.
KanbanWorkflowService::getPreviousWorkflowStateId public function Get ID of the previous workflow state.
KanbanWorkflowService::getWorkflowStateHistory public function Gets the workflow state history of a given entity.
KanbanWorkflowService::getWorkflowStates public static function Get Workflow States.
KanbanWorkflowService::onEntityPresave public function Acts upon a entity presave.
KanbanWorkflowService::__construct public function Constructs a new NewsService object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.