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

Issue #3525841: Introduce WorkflowTargetEntity::getWorkflow()

parent 430c793f
No related branches found
No related tags found
No related merge requests found
Pipeline #512586 passed with warnings
...@@ -45,23 +45,17 @@ class WorkflowHistoryAccess implements AccessInterface { ...@@ -45,23 +45,17 @@ class WorkflowHistoryAccess implements AccessInterface {
return $value; return $value;
} }
// When having multiple workflows per bundle,
// use Views display 'Workflow history per entity' instead!
$fields = _workflow_info_fields($entity, $entity_type, $entity_bundle, $field_name);
if (!$fields) {
return AccessResult::forbidden();
}
/* /*
* Determine if user has Access. Fill the cache. * Determine if user has Access. Fill the cache.
*/ */
// Note: for multiple workflow_fields per bundle, use Views instead! // N.B. For multiple workflow_fields per bundle, use Views instead!
// @todo Use proper 'WORKFLOW_TYPE' permissions for workflow_tab_access.
// N.B. Keep aligned between WorkflowState, ~Transition, ~HistoryAccess. // N.B. Keep aligned between WorkflowState, ~Transition, ~HistoryAccess.
// @todo Use proper 'WORKFLOW_TYPE' permissions for workflow_tab_access.
$access_result = AccessResult::forbidden(); $access_result = AccessResult::forbidden();
$is_owner = WorkflowRole::isOwner($account, $entity); $is_owner = WorkflowRole::isOwner($account, $entity);
$fields = _workflow_info_fields($entity, $entity_type, $entity_bundle, $field_name);
foreach ($fields as $definition) { foreach ($fields as $definition) {
$field_name = $definition->getName();
$type_id = $definition->getSetting('workflow_type'); $type_id = $definition->getSetting('workflow_type');
$access_result = match (TRUE) { $access_result = match (TRUE) {
$account->hasPermission("access any $type_id workflow_transion overview") $account->hasPermission("access any $type_id workflow_transion overview")
...@@ -74,6 +68,7 @@ class WorkflowHistoryAccess implements AccessInterface { ...@@ -74,6 +68,7 @@ class WorkflowHistoryAccess implements AccessInterface {
=> AccessResult::forbidden(), => AccessResult::forbidden(),
}; };
$field_name = $definition->getName();
$access[$uid][$entity_type][$entity_id][$field_name] = $access_result; $access[$uid][$entity_type][$entity_id][$field_name] = $access_result;
} }
......
...@@ -112,13 +112,12 @@ class WorkflowTargetEntity { ...@@ -112,13 +112,12 @@ class WorkflowTargetEntity {
public static function getCreationState(EntityInterface $entity, $field_name) { public static function getCreationState(EntityInterface $entity, $field_name) {
$state = NULL; $state = NULL;
/** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity */ $wid = WorkflowTargetEntity::getWorkflowId($entity, $field_name);
/** @var \Drupal\workflow\Entity\WorkflowInterface $workflow */ if (!$wid) {
$workflow = $entity->{$field_name}?->first()?->getWorkflow(); \Drupal::messenger()->addError(t('Workflow %wid cannot be loaded. Contact your system administrator.', ['%wid' => $wid]));
if (!$workflow) {
\Drupal::messenger()->addError(t('Workflow %wid cannot be loaded. Contact your system administrator.', ['%wid' => $workflow?->id() ?? '']));
} }
else { else {
$workflow = Workflow::load($wid);
$state = $workflow->getCreationState(); $state = $workflow->getCreationState();
} }
...@@ -279,6 +278,54 @@ class WorkflowTargetEntity { ...@@ -279,6 +278,54 @@ class WorkflowTargetEntity {
return $sid; return $sid;
} }
/**
* Gets the Workflow for a given $entity and $field_name.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $field_name
*
* @return \Drupal\workflow\Entity\Workflow
* The Workflow entity of the field.
*
* @see WorkflowTargetEntity::getWorkflowId()
*/
public static function getWorkflow(EntityInterface $entity, $field_name) {
$workflow = NULL;
$wid = WorkflowTargetEntity::getWorkflowId($entity, $field_name);
if (!$wid) {
\Drupal::messenger()->addError(t('Workflow %wid cannot be loaded. Contact your system administrator.', ['%wid' => $wid]));
}
else {
$workflow = Workflow::load($wid);
}
return $workflow;
}
/**
* Gets the Workflow ID for a given $entity and $field_name.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $field_name
*
* @return \Drupal\workflow\Entity\WorkflowState
* The creation State for the Workflow of the field.
*
* @see WorkflowTargetEntity::getCurrentStateId()
*/
public static function getWorkflowId(EntityInterface $entity, $field_name) {
$wid = NULL;
/** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity */
// Sometimes, no first item is available, so read wid from storage.
// $workflow = $entity->{$field_name}?->first()?->getWorkflow();
$definition = $entity->{$field_name}->getFieldDefinition();
$field_storage_definition = $definition->getFieldStorageDefinition();
$wid = $field_storage_definition->getSetting('workflow_type');
return $wid;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
......
...@@ -173,9 +173,10 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition ...@@ -173,9 +173,10 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
$entity = $values['entity'] ?? NULL; $entity = $values['entity'] ?? NULL;
$field_name = $values['field_name']; $field_name = $values['field_name'];
$wid = $values['wid'];
// Fieldname may not exist on entity, but only on Comment with workflow. // Fieldname may not exist on entity, but only on Comment with workflow.
// E.g, when adding comment with workflow, on entity w/o workflow field. // E.g, when adding comment with workflow, on entity w/o workflow field.
$wid = $entity?->{$field_name}?->first()?->getWorkflowId(); $wid ??= $entity?->{$field_name}?->first()?->getWorkflowId();
if ($entity && $wid) { if ($entity && $wid) {
unset($values['entity']); unset($values['entity']);
......
...@@ -341,34 +341,24 @@ class WorkflowEntityHooks { ...@@ -341,34 +341,24 @@ class WorkflowEntityHooks {
$original_entity = WorkflowTargetEntity::getOriginal($entity); $original_entity = WorkflowTargetEntity::getOriginal($entity);
foreach ($field_names as $field_name => $label) { foreach ($field_names as $field_name => $label) {
/** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
// @todo Transition is created in widget or WorkflowTransitionForm.
// @todo $transition = workflow_get_transition($entity, $field_name);
// Transition may be empty on node with mismatched CommentWithWorkflow.
$transition = $entity->{$field_name}->first()?->getTransition();
if (!$transition) {
// We come from creating/editing an entity via entity_form,
// with core widget or hidden Workflow widget.
// @todo D8: From an Edit form with hidden widget.
/* @noinspection PhpUndefinedFieldInspection */
if ($original_entity) { if ($original_entity) {
// Editing a Node with hidden Widget. State change not possible, so bail out. // Editing a Node with hidden Widget. State change not possible, so bail out.
// $entity->{$field_name}->value = $original_entity->{$field_name}->value; // $entity->{$field_name}->value = $original_entity->{$field_name}->value;
// continue; // continue;
} }
// Creating a Node with hidden Workflow Widget. Generate valid first transition. /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
$old_sid = WorkflowTargetEntity::getPreviousStateId($entity, $field_name); // Transition may be empty on node with mismatched CommentWithWorkflow.
$new_sid = $entity->{$field_name}->value; $transition = $entity->{$field_name}?->first()?->getTransition();
/** @var \Drupal\workflow\Entity\WorkflowInterface $workflow */ // Note: Field is empty if node created before module installation.
$workflow = $entity->{$field_name}->first()?->getWorkflow(); // We come from creating/editing an entity via entity_form,
if ($workflow && !$new_sid) { // with core widget or hidden Workflow widget.
$user = workflow_current_user(); // Or from WorkflowOnComment with core widget.
$new_sid = $workflow->getFirstSid($entity, $field_name, $user); // @todo D8: From an Edit form with hidden widget.
} $transition ??= WorkflowTransition::create([
$transition = WorkflowTransition::create([$old_sid, 'field_name' => $field_name]) 'entity' => $entity,
?->setValues($new_sid, NULL, NULL, NULL, TRUE); 'field_name' => $field_name,
} ]);
// We come from Content/Comment edit page, from widget. // We come from Content/Comment edit page, from widget.
// Set the just-saved entity explicitly. // Set the just-saved entity explicitly.
......
...@@ -308,6 +308,7 @@ class WorkflowItem extends ListItemBase { ...@@ -308,6 +308,7 @@ class WorkflowItem extends ListItemBase {
$transition ??= WorkflowTransition::create([ $transition ??= WorkflowTransition::create([
'entity' => $this->getEntity(), 'entity' => $this->getEntity(),
'field_name' => $this->getParent()->getName(), 'field_name' => $this->getParent()->getName(),
'wid' => $this->getWorkflowId(),
]); ]);
return $transition; return $transition;
} }
...@@ -438,10 +439,12 @@ class WorkflowItem extends ListItemBase { ...@@ -438,10 +439,12 @@ class WorkflowItem extends ListItemBase {
} }
// Get the allowed new states for the entity's current state. // Get the allowed new states for the entity's current state.
$field_name = $this->getFieldDefinition()->getName(); $transition = $this->getTransition();
$state = $this->getState(); $allowed_options = $transition?->getSettableOptions($account, 'to_sid');
// @todo use workflow_state_allowed_values, for executed transition. // $field_name = $this->getFieldDefinition()->getName();
$allowed_options = $state?->getOptions($entity, $field_name, NULL, FALSE); // $state = $this->getState();
// @done use workflow_state_allowed_values, for executed transition.
// $allowed_options = $state?->getOptions($entity, $field_name, NULL, FALSE);
return $allowed_options ?? []; return $allowed_options ?? [];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment