Commit 271f8256 authored by bojanz's avatar bojanz

Expand and improve the StateItemInterface.

parent 4105e2d7
......@@ -26,7 +26,7 @@ use Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint;
* default_formatter = "list_default"
* )
*/
class StateItem extends FieldItemBase implements OptionsProviderInterface {
class StateItem extends FieldItemBase implements StateItemInterface, OptionsProviderInterface {
/**
* A cache of loaded workflows, keyed by field definition hash.
......@@ -209,7 +209,7 @@ class StateItem extends FieldItemBase implements OptionsProviderInterface {
}
/**
* Gets the next allowed states for the current field value.
* Gets the next allowed states for the given field value.
*
* @param string $value
* The field value, representing the state id.
......@@ -249,6 +249,24 @@ class StateItem extends FieldItemBase implements OptionsProviderInterface {
return static::$workflows[$definition_id];
}
/**
* {@inheritdoc}
*/
public function getTransitions() {
$transitions = [];
if ($workflow = $this->getWorkflow()) {
$transitions = $workflow->getAllowedTransitions($this->value, $this->getEntity());
}
return $transitions;
}
/**
* {@inheritdoc}
*/
public function applyTransition(WorkflowTransition $transition) {
$this->setValue(['value' => $transition->getToState()->getId()]);
}
/**
* Loads the workflow used by the current field.
*
......
......@@ -7,21 +7,35 @@
namespace Drupal\commerce_workflow\Plugin\Field\FieldType;
use Drupal\Core\TypedData\OptionsProviderInterface;
/**
* Defines the interface for state item fields.
*/
interface StateItemInterface extends OptionsProviderInterface {
interface StateItemInterface {
/**
* Gets the workflow used by the current field.
* Gets the workflow used by the field.
*
* @return \Drupal\commerce_workflow\Plugin\Workflow\WorkflowInterface|false
* The workflow, or FALSE if unknown at this time.
*/
public function getWorkflow();
/**
* Gets the allowed transitions for the current state.
*
* @return \Drupal\commerce_workflow\Plugin\Workflow\WorkflowTransition[]
* The allowed transitions.
*/
public function getTransitions();
/**
* Applies the given transition, changing the current state.
*
* @param \Drupal\commerce_workflow\Plugin\Workflow\WorkflowTransition $transition
* The transition to apply.
*/
public function applyTransition(WorkflowTransition $transition);
/**
* Gets whether the current state is valid.
*
......
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