OptionsProviderInterface has the following in its docblock:
* For example, in an workflow scenario, the settable values for a state field
* might depend on the currently set state, while possible values are all
* states. Thus settable values would be used in an editing context, while
* possible values would be used for presenting filtering options in a search.
Great, that's precisely what I'm doing, creating a state field, and returning the next allowed states in getSettableOptions().
However, turns out that the field value is empty/unpopulated, and that I need to do:
$field_name = $this->getFieldDefinition()->getName();
$value = $this->getEntity()->{$field_name}->value;
This is obviously a bug that occurs when creating a cloned field instance.
Here's the call stack:
- OptionsSelectWidget::formElement
- OptionsWidgetBase::getOptions
- FieldStorageConfig::getOptionsProvider
- FieldTypePluginManager::createFieldItem
- TypedDataManager::getPropertyInstance
Looks like the bug occurs in TypedDataManager. The FieldTypePluginManager passes $items which contains the value, but typed data manager's getPropertyInstance() doesn't seem to do anything with it (it has its own $value argument which FieldTypePluginManager is not passing).