View source
<?php
namespace Drupal\workflow\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\workflow\WorkflowURLRouteParametersTrait;
class Workflow extends ConfigEntityBase implements WorkflowInterface {
use MessengerTrait;
use StringTranslationTrait;
use WorkflowURLRouteParametersTrait;
public $id;
public $label;
public $options = [];
private $creation_state = NULL;
private $creation_sid = 0;
public $states = [];
public $transitions = [];
protected $module = 'workflow';
public function save() {
$status = parent::save();
if ($status == SAVED_NEW) {
$this
->getCreationState();
}
return $status;
}
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
foreach ($entities as &$workflow) {
$workflow
->getStates();
$workflow
->getCreationState();
}
}
public function delete() {
if (!$this
->isDeletable()) {
$message = $this
->t('Workflow %workflow is not Deletable. Please delete the field where this workflow type is referred', [
'%workflow' => $this
->label(),
]);
$this
->messenger()
->addError($message);
return;
}
else {
foreach ($this
->getStates(TRUE) as $state) {
$state
->deactivate('');
$state
->delete();
}
parent::delete();
}
}
public function isValid() {
$is_valid = TRUE;
$states = $this
->getStates(FALSE);
if (count($states) < 1) {
$message = $this
->t('Workflow %workflow has no states defined, so it cannot be assigned to content yet.', [
'%workflow' => $this
->label(),
]);
$this
->messenger()
->addWarning($message);
$is_valid = FALSE;
}
$transitions = $this
->getTransitionsByStateId($this
->getCreationSid(), '');
if (count($transitions) < 1) {
$message = $this
->t('Workflow %workflow has no transitions defined, so it cannot be assigned to content yet.', [
'%workflow' => $this
->label(),
]);
$this
->messenger()
->addWarning($message);
$is_valid = FALSE;
}
return $is_valid;
}
public function isDeletable() {
foreach (_workflow_info_fields() as $field_info) {
if ($field_info
->getSetting('workflow_type') == $this
->id()) {
return FALSE;
}
}
return TRUE;
}
public function getWorkflowId() {
return $this
->id();
}
public function createState($sid, $save = TRUE) {
$wid = $this
->id();
$state = WorkflowState::load($sid);
if (!$state || $wid != $state
->getWorkflowId()) {
$values = [
'id' => $sid,
'wid' => $wid,
];
$state = WorkflowState::create($values);
if ($save) {
$state
->save();
}
}
$this->states[$state
->id()] = $state;
return $state;
}
public function getCreationState() {
if (!$this->creation_state) {
foreach ($this
->getStates(TRUE) as $state) {
if ($state
->isCreationState()) {
$this->creation_state = $state;
$this->creation_sid = $state
->id();
}
}
}
if (\Drupal::isConfigSyncing()) {
}
elseif (!$this->creation_state) {
$state = $this
->createState(WORKFLOW_CREATION_STATE_NAME);
$this->creation_state = $state;
$this->creation_sid = $state
->id();
}
return $this->creation_state;
}
public function getCreationSid() {
if (!$this->creation_sid) {
$state = $this
->getCreationState();
return $state
->id();
}
return $this->creation_sid;
}
public function getFirstSid(EntityInterface $entity, $field_name, AccountInterface $user, $force = FALSE) {
$creation_state = $this
->getCreationState();
$options = $creation_state
->getOptions($entity, $field_name, $user, $force);
if ($options) {
$keys = array_keys($options);
$sid = $keys[0];
}
else {
$this
->messenger()
->addError($this
->t('There are no workflow states available. Please notify your site administrator.'));
$sid = 0;
}
return $sid;
}
public function getNextSid(EntityInterface $entity, $field_name, AccountInterface $user, $force = FALSE) {
$current_sid = WorkflowManager::getCurrentStateId($entity, $field_name);
$current_state = WorkflowState::load($current_sid);
$options = $current_state
->getOptions($entity, $field_name, $user, $force);
$flag = $current_state
->isCreationState();
$new_sid = $current_state
->id();
foreach ($options as $sid => $name) {
if ($flag) {
$new_sid = $sid;
break;
}
if ($sid == $current_state
->id()) {
$flag = TRUE;
}
}
return $new_sid;
}
public function getStates($all = FALSE, $reset = FALSE) {
$wid = $this
->id();
if ($reset) {
$this->states = $wid ? WorkflowState::loadMultiple([], $wid, $reset) : [];
}
elseif ($this->states === NULL) {
$this->states = $wid ? WorkflowState::loadMultiple([], $wid, $reset) : [];
}
elseif ($this->states === []) {
$this->states = $wid ? WorkflowState::loadMultiple([], $wid, $reset) : [];
}
$states = [];
foreach ($this->states as $state) {
$id = $state
->id();
if ($all === TRUE) {
$states[$id] = $state;
}
elseif ($all === FALSE && ($state
->isActive() && !$state
->isCreationState())) {
$states[$id] = $state;
}
elseif ($all == 'CREATION' && ($state
->isActive() || $state
->isCreationState())) {
$states[$id] = $state;
}
else {
}
}
return $states;
}
public function getState($sid) {
$wid = $this
->id();
$state = WorkflowState::load($sid);
if (!$wid || $wid == $state
->getWorkflowId()) {
return $state;
}
return NULL;
}
public function createTransition($from_sid, $to_sid, array $values = []) {
$config_transition = NULL;
$transitions = $this
->getTransitionsByStateId($from_sid, $to_sid);
if ($transitions) {
$config_transition = reset($transitions);
}
else {
$values['wid'] = $this
->id();
$values['from_sid'] = $from_sid;
$values['to_sid'] = $to_sid;
$config_transition = WorkflowConfigTransition::create($values);
$config_transition
->save();
}
$this->transitions[$config_transition
->id()] = $config_transition;
return $config_transition;
}
public function sortTransitions() {
uasort($this->transitions, [
'Drupal\\workflow\\Entity\\WorkflowConfigTransition',
'sort',
]);
}
public function getTransitions(array $ids = NULL, array $conditions = []) {
$config_transitions = [];
$from_sid = isset($conditions['from_sid']) ? $conditions['from_sid'] : FALSE;
$to_sid = isset($conditions['to_sid']) ? $conditions['to_sid'] : FALSE;
$states = $this
->getStates('CREATION');
if (!$this->transitions) {
$this->transitions = WorkflowConfigTransition::loadMultiple($ids);
$this
->sortTransitions();
}
foreach ($this->transitions as &$config_transition) {
if (!isset($states[$config_transition
->getFromSid()])) {
}
elseif ($from_sid && $from_sid != $config_transition
->getFromSid()) {
}
elseif ($to_sid && $to_sid != $config_transition
->getToSid()) {
}
else {
$config_transitions[$config_transition
->id()] = $config_transition;
}
}
return $config_transitions;
}
public function getTransitionsById($tid) {
return $this
->getTransitions([
$tid,
]);
}
public function getTransitionsByStateId($from_sid, $to_sid) {
$conditions = [
'from_sid' => $from_sid,
'to_sid' => $to_sid,
];
return $this
->getTransitions(NULL, $conditions);
}
protected $defaultSettingsMerged = FALSE;
public static function defaultSettings() {
return [
'name_as_title' => 0,
'fieldset' => 0,
'options' => 'radios',
'schedule_enable' => 1,
'schedule_timezone' => 1,
'always_update_entity' => 0,
'comment_log_node' => '1',
'watchdog_log' => TRUE,
];
}
public function getSettings() {
if (!$this->defaultSettingsMerged) {
$this
->mergeDefaults();
}
return $this->options;
}
public function getSetting($key) {
if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->options)) {
$this
->mergeDefaults();
}
return isset($this->options[$key]) ? $this->options[$key] : NULL;
}
protected function mergeDefaults() {
$this->options += static::defaultSettings();
$this->defaultSettingsMerged = TRUE;
}
public function setSettings(array $settings) {
$this->options = $settings;
$this->defaultSettingsMerged = FALSE;
return $this;
}
public function setSetting($key, $value) {
$this->options[$key] = $value;
return $this;
}
}