Commit 690edddd authored by bojanz's avatar bojanz

Complete the workflow plugin API.

parent 79da4774
......@@ -124,18 +124,24 @@ class StateItem extends FieldItemBase implements OptionsProviderInterface {
* {@inheritdoc}
*/
public function getSettableValues(AccountInterface $account = NULL) {
$states = $this->getWorkflow()->getStates();
return array_keys($states);
return array_keys($this->getSettableOptions($account));
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
$states = $this->getWorkflow()->getStates();
$state_labels = array_map(function ($state) {
return $state->getLabel();
}, $states);
$workflow = $this->getWorkflow();
$state_labels = [];
if ($this->value) {
// The current state is always allowed.
$state_labels[$this->value] = $workflow->getState($this->value)->getLabel();
}
$transitions = $workflow->getAllowedTransitions($this->value, $this->getEntity());
foreach ($transitions as $transition) {
$state = $transition->getToState();
$state_labels[$state->getId()] = $state->getLabel();
}
return $state_labels;
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\commerce_workflow\Plugin\Workflow;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\PluginBase;
/**
......@@ -91,4 +92,51 @@ class Workflow extends PluginBase implements WorkflowInterface {
return isset($this->transitions[$id]) ? $this->transitions[$id] : NULL;
}
/**
* {@inheritdoc}
*/
public function getPossibleTransitions($state_id) {
if (empty($state_id)) {
return $this->transitions;
}
$possible_transitions = [];
foreach ($this->transitions as $id => $transition) {
if (array_key_exists($state_id, $transition->getFromStates())) {
$possible_transitions[$id] = $transition;
}
}
return $possible_transitions;
}
/**
* {@inheritdoc}
*/
public function getAllowedTransitions($state_id, EntityInterface $entity) {
$allowed_transitions = [];
foreach ($this->getPossibleTransitions($state_id) as $transition_id => $transition) {
if ($this->isTransitionAllowed($transition, $entity)) {
$allowed_transitions[$transition_id] = $transition;
}
}
return $allowed_transitions;
}
/**
* Gets whether the given transition is allowed.
*
* @param \Drupal\commerce_workflow\Plugin\Workflow\WorkflowTransition $transition
* The transition.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The parent entity.
*
* @return bool
* TRUE if the transition is allowed, FALSE otherwise.
*/
protected function isTransitionAllowed(WorkflowTransition $transition, EntityInterface $entity) {
// @todo
return TRUE;
}
}
......@@ -7,6 +7,8 @@
namespace Drupal\commerce_workflow\Plugin\Workflow;
use Drupal\Core\Entity\EntityInterface;
/**
* Defines the interface for workflows.
*/
......@@ -66,4 +68,31 @@ interface WorkflowInterface {
*/
public function getTransition($id);
/**
* Gets the possible workflow transitions for the given state id.
*
* Note that a possible transition might not be allowed (because of a guard
* returning false).
*
* @param string $state_id
* The state id.
*
* @return \Drupal\commerce_workflow\Plugin\Workflow\WorkflowTransition[]
* The possible transitions.
*/
public function getPossibleTransitions($state_id);
/**
* Gets the allowed workflow transitions for the given state id.
*
* @param string $state_id
* The state id.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The parent entity.
*
* @return \Drupal\commerce_workflow\Plugin\Workflow\WorkflowTransition[]
* The allowed transitions.
*/
public function getAllowedTransitions($state_id, EntityInterface $entity);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment