Issue #2371853 by tim.plunkett, larowlan: Add more helper methods around temporary FAPI storage
parent
0d720363d8
commit
9bb4f4cfd4
|
@ -44,8 +44,7 @@ abstract class ConditionPluginBase extends ExecutablePluginBase implements Condi
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||||
$temporary = $form_state->getTemporary();
|
$contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
|
||||||
$contexts = isset($temporary['gathered_contexts']) ? $temporary['gathered_contexts'] : [];
|
|
||||||
$form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
|
$form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
|
||||||
$form['negate'] = array(
|
$form['negate'] = array(
|
||||||
'#type' => 'checkbox',
|
'#type' => 'checkbox',
|
||||||
|
|
|
@ -354,7 +354,7 @@ class FormState implements FormStateInterface {
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $temporary;
|
protected $temporary = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tracks if the form has finished validation.
|
* Tracks if the form has finished validation.
|
||||||
|
@ -711,6 +711,31 @@ class FormState implements FormStateInterface {
|
||||||
return $this->temporary;
|
return $this->temporary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function &getTemporaryValue($key) {
|
||||||
|
$value = &NestedArray::getValue($this->temporary, (array) $key);
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function setTemporaryValue($key, $value) {
|
||||||
|
NestedArray::setValue($this->temporary, (array) $key, $value, TRUE);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function hasTemporaryValue($key) {
|
||||||
|
$exists = NULL;
|
||||||
|
NestedArray::getValue($this->temporary, (array) $key, $exists);
|
||||||
|
return $exists;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -933,6 +933,47 @@ interface FormStateInterface {
|
||||||
*/
|
*/
|
||||||
public function getTemporary();
|
public function getTemporary();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an arbitrary value from temporary storage.
|
||||||
|
*
|
||||||
|
* @param string|array $key
|
||||||
|
* Properties are often stored as multi-dimensional associative arrays. If
|
||||||
|
* $key is a string, it will return $temporary[$key]. If $key is an array,
|
||||||
|
* each element of the array will be used as a nested key. If
|
||||||
|
* $key = ['foo', 'bar'] it will return $temporary['foo']['bar'].
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
* A reference to the value for that key, or NULL if the property does
|
||||||
|
* not exist.
|
||||||
|
*/
|
||||||
|
public function &getTemporaryValue($key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets an arbitrary value in temporary storage.
|
||||||
|
*
|
||||||
|
* @param string|array $key
|
||||||
|
* Properties are often stored as multi-dimensional associative arrays. If
|
||||||
|
* $key is a string, it will use $temporary[$key] = $value. If $key is an
|
||||||
|
* array, each element of the array will be used as a nested key. If
|
||||||
|
* $key = ['foo', 'bar'] it will use $temporary['foo']['bar'] = $value.
|
||||||
|
* @param mixed $value
|
||||||
|
* The value to set.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setTemporaryValue($key, $value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if a temporary value is present.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* Properties are often stored as multi-dimensional associative arrays. If
|
||||||
|
* $key is a string, it will return isset($temporary[$key]). If $key is an
|
||||||
|
* array, each element of the array will be used as a nested key. If
|
||||||
|
* $key = ['foo', 'bar'] it will return isset($temporary['foo']['bar']).
|
||||||
|
*/
|
||||||
|
public function hasTemporaryValue($key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the form element that triggered submission.
|
* Sets the form element that triggered submission.
|
||||||
*
|
*
|
||||||
|
|
|
@ -104,9 +104,7 @@ class BlockForm extends EntityForm {
|
||||||
|
|
||||||
// Store the gathered contexts in the form state for other objects to use
|
// Store the gathered contexts in the form state for other objects to use
|
||||||
// during form building.
|
// during form building.
|
||||||
$temporary = $form_state->getTemporary();
|
$form_state->setTemporaryValue('gathered_contexts', $this->dispatcher->dispatch(BlockEvents::ADMINISTRATIVE_CONTEXT, new BlockContextEvent())->getContexts());
|
||||||
$temporary['gathered_contexts'] = $this->dispatcher->dispatch(BlockEvents::ADMINISTRATIVE_CONTEXT, new BlockContextEvent())->getContexts();
|
|
||||||
$form_state->setTemporary($temporary);
|
|
||||||
|
|
||||||
$form['#tree'] = TRUE;
|
$form['#tree'] = TRUE;
|
||||||
$form['settings'] = $entity->getPlugin()->buildConfigurationForm(array(), $form_state);
|
$form['settings'] = $entity->getPlugin()->buildConfigurationForm(array(), $form_state);
|
||||||
|
|
|
@ -514,6 +514,22 @@ class FormStateTest extends UnitTestCase {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getTemporaryValue
|
||||||
|
* @covers ::hasTemporaryValue
|
||||||
|
* @covers ::setTemporaryValue
|
||||||
|
*/
|
||||||
|
public function testTemporaryValue() {
|
||||||
|
$form_state = New FormState();
|
||||||
|
$this->assertFalse($form_state->hasTemporaryValue('rainbow_sparkles'));
|
||||||
|
$form_state->setTemporaryValue('rainbow_sparkles', 'yes please');
|
||||||
|
$this->assertSame($form_state->getTemporaryValue('rainbow_sparkles'), 'yes please');
|
||||||
|
$this->assertTrue($form_state->hasTemporaryValue('rainbow_sparkles'), TRUE);
|
||||||
|
$form_state->setTemporaryValue(array('rainbow_sparkles', 'magic_ponies'), 'yes please');
|
||||||
|
$this->assertSame($form_state->getTemporaryValue(array('rainbow_sparkles', 'magic_ponies')), 'yes please');
|
||||||
|
$this->assertTrue($form_state->hasTemporaryValue(array('rainbow_sparkles', 'magic_ponies')), TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue