KanbanWorkflowService.php in Content Planner 8
File
modules/content_kanban/src/KanbanWorkflowService.php
View source
<?php
namespace Drupal\content_kanban;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\workflows\Entity\Workflow;
class KanbanWorkflowService {
use StringTranslationTrait;
protected $database;
protected $moderationInformation;
protected $kanbanLogService;
public function __construct(Connection $database, ModerationInformationInterface $moderation_information, KanbanLogService $kanban_log_service) {
$this->database = $database;
$this->moderationInformation = $moderation_information;
$this->kanbanLogService = $kanban_log_service;
}
public function onEntityPresave(ContentEntityInterface $entity, AccountInterface $user) {
if ($this->moderationInformation
->isModeratedEntity($entity)) {
$current_state = $this
->getCurrentStateId($entity);
$prev_state = $this
->getPreviousWorkflowStateId($entity);
if ($current_state && $prev_state) {
$name = $this
->t('Workflow State change on Entity')
->render();
$workflow = $this->moderationInformation
->getWorkflowForEntity($entity);
$this->kanbanLogService
->createLogEntity($name, $user
->id(), $entity
->id(), $entity
->getEntityTypeId(), $workflow
->id(), $prev_state, $current_state);
}
}
}
public function getCurrentStateId(ContentEntityInterface $entity) {
return $entity->moderation_state->value;
}
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;
}
public static function getWorkflowStates(Workflow $workflow) {
$states = [];
$type_settings = $workflow
->get('type_settings');
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;
}
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();
}
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;
}
}