Commit 97467afe authored by bojanz's avatar bojanz

Rework StateItem value handling.

1) Default to the initial state, so that the value is never empty.
2) Work around core bug in getSettableOptions().
parent 8ba3137c
......@@ -101,6 +101,17 @@ class StateItem extends FieldItemBase implements OptionsProviderInterface {
return $this->value === NULL || $this->value === '';
}
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
if ($workflow = $this->getWorkflow()) {
$initial_state = reset($workflow->getStates());
$this->setValue(['value' => $initial_state->getId()], $notify);
}
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -140,12 +151,16 @@ class StateItem extends FieldItemBase implements OptionsProviderInterface {
// The workflow is not known yet, the field is probably being created.
return [];
}
$state_labels = [];
if ($this->value) {
$entity = $this->getEntity();
// $this->value is unpopulated due to https://www.drupal.org/node/2629932
$field_name = $this->getFieldDefinition()->getName();
$value = $entity->get($field_name)->value;
$state_labels = [
// The current state is always allowed.
$state_labels[$this->value] = $workflow->getState($this->value)->getLabel();
}
$transitions = $workflow->getAllowedTransitions($this->value, $this->getEntity());
$value => $workflow->getState($value)->getLabel(),
];
$transitions = $workflow->getAllowedTransitions($value, $entity);
foreach ($transitions as $transition) {
$state = $transition->getToState();
$state_labels[$state->getId()] = $state->getLabel();
......
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