Skip to content
Snippets Groups Projects
Commit bbceea16 authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #3525841: Introduce WorkflowItem::getWorkflowId()

parent 094e6a31
No related branches found
No related tags found
No related merge requests found
Pipeline #503022 passed with warnings
......@@ -52,8 +52,6 @@ class WorkflowTransitionTimestamp extends FormElement {
return $timestamp;
}
// Continue with parsing element values when saving
// a scheduled transition. (@todo Why is default value empty?).
$input = $form_state->getValue(['timestamp', 0, 'value']);
if (!is_array($input)) {
// Editing the comments of an executed transition.
......
......
......@@ -113,14 +113,10 @@ class WorkflowTargetEntity {
$state = NULL;
/** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity */
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_config */
$field_config = $entity->get($field_name)->getFieldDefinition();
$field_storage = $field_config->getFieldStorageDefinition();
$wid = $field_storage->getSetting('workflow_type');
/** @var \Drupal\workflow\Entity\WorkflowInterface $workflow */
$workflow = $wid ? Workflow::load($wid) : NULL;
$workflow = $entity->{$field_name}?->first()?->getWorkflow();
if (!$workflow) {
\Drupal::messenger()->addError(t('Workflow %wid cannot be loaded. Contact your system administrator.', ['%wid' => $wid ?? '']));
\Drupal::messenger()->addError(t('Workflow %wid cannot be loaded. Contact your system administrator.', ['%wid' => $workflow?->id() ?? '']));
}
else {
$state = $workflow->getCreationState();
......
......
......@@ -7,7 +7,9 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\OptGroup;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\Url;
use Drupal\options\Plugin\Field\FieldType\ListItemBase;
use Drupal\workflow\Entity\Workflow;
......@@ -15,6 +17,7 @@ use Drupal\workflow\Entity\WorkflowInterface;
use Drupal\workflow\Entity\WorkflowState;
use Drupal\workflow\Entity\WorkflowTransition;
use Drupal\workflow\Entity\WorkflowTransitionInterface;
use Drupal\workflow\WorkflowTypeAttributeTrait;
/**
* Plugin implementation of the 'workflow' field type.
......@@ -34,6 +37,20 @@ use Drupal\workflow\Entity\WorkflowTransitionInterface;
*/
class WorkflowItem extends ListItemBase {
use MessengerTrait;
/*
* Add variables and get/set methods for Workflow property.
*/
use WorkflowTypeAttributeTrait;
/**
* {@inheritdoc}
*/
public function __construct(ComplexDataDefinitionInterface $definition, $name = NULL, ?TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
// Avoid exception in WorkflowTypeAttributeTrait::getWorkflowId();
$this->setWorkflowId($this->getSetting('workflow_type'));
}
/**
* {@inheritdoc}
......@@ -117,7 +134,7 @@ class WorkflowItem extends ListItemBase {
// Create list of all Workflow types. Include an initial empty value.
$workflow_options = workflow_allowed_workflow_names(FALSE);
$field_storage = $this->getFieldDefinition()->getFieldStorageDefinition();
$wid = $this->getSetting('workflow_type');
$wid = $this->getWorkflowId();
// Set the required workflow_type on 'comment' fields.
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
......@@ -253,7 +270,7 @@ class WorkflowItem extends ListItemBase {
$state = WorkflowState::load($sid)
?? WorkflowState::create([
'id' => $sid,
'wid' => $this->getFieldDefinition()->getSetting('workflow_type'),
'wid' => $this->getWorkflowId(),
]);
return $state;
}
......@@ -332,7 +349,7 @@ class WorkflowItem extends ListItemBase {
protected function allowedValuesString($states) {
$lines = [];
$wid = $this->getSetting('workflow_type');
$wid = $this->getWorkflowId();
$previous_wid = -1;
/** @var \Drupal\workflow\Entity\WorkflowState $state */
......@@ -385,7 +402,7 @@ class WorkflowItem extends ListItemBase {
$allowed_options = [];
// When we are initially on the Storage settings form, no wid is set, yet.
if (!$wid = $this->getSetting('workflow_type')) {
if (!$wid = $this->getWorkflowId()) {
return $allowed_options;
}
......@@ -413,7 +430,7 @@ class WorkflowItem extends ListItemBase {
$allowed_options = [];
// When we are initially on the Storage settings form, no wid is set, yet.
if (!$wid = $this->getSetting('workflow_type')) {
if (!$wid = $this->getWorkflowId()) {
return $allowed_options;
}
......@@ -428,10 +445,8 @@ class WorkflowItem extends ListItemBase {
$state = $this->getState();
// Get the allowed new states for the entity's current state.
// @todo use workflow_state_allowed_values, for executed transition.
$allowed_options = ($state)
? $state->getOptions($entity, $field_name, $user, FALSE)
: [];
return $allowed_options;
$allowed_options = $state?->getOptions($entity, $field_name, $user, FALSE);
return $allowed_options ?? [];
}
}
......@@ -8,7 +8,7 @@ use Drupal\workflow\Entity\WorkflowInterface;
/**
* Wrapper methods for Workflow* objects.
*
* Using this trait will add getWorkflow(), getWorkflowID() and setWorkflow()
* This adds getWorkflow(), getWorkflowId(), setWorkflow(), setWorkflowId()
* methods to the class.
*
* @ingroup workflow
......@@ -89,16 +89,29 @@ trait WorkflowTypeAttributeTrait {
public function getWorkflowId() {
/** @var \Drupal\Core\Entity\ContentEntityBase $this */
if (!empty($this->wid)) {
// WorkflowState.
return $this->wid;
}
$value = $this->get('wid');
try {
$this->wid = match (TRUE) {
is_string($value) => $value,
// WorkflowItem.
$this->wid = $this->getSetting('workflow_type');
if ($this->wid) {
return $this->wid;
}
}
catch (\Error $e) {
// Error: Call to undefined method Drupal\workflow\Entity\WorkflowTransition::getSetting()
}
try {
// WorkflowTransition.
$value = $this->get('wid');
$this->wid = match (TRUE) {
// In WorkflowTransition.
is_object($value) => $value->getValue()[0]['target_id'] ?? '',
// In ...
is_string($value) => $value,
};
}
catch (\UnhandledMatchError $e) {
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment