Commit ee6ddbe8 authored by alexpott's avatar alexpott

Issue #2332389 by tim.plunkett: Finish adding methods to FormStateInterface.

parent fe85f9be
...@@ -806,12 +806,9 @@ function install_tasks_to_display($install_state) { ...@@ -806,12 +806,9 @@ function install_tasks_to_display($install_state) {
function install_get_form($form_id, array &$install_state) { function install_get_form($form_id, array &$install_state) {
// Ensure the form will not redirect, since install_run_tasks() uses a custom // Ensure the form will not redirect, since install_run_tasks() uses a custom
// redirection logic. // redirection logic.
$form_state = new FormState(array( $form_state = (new FormState())
'build_info' => array( ->addBuildInfo('args', [&$install_state])
'args' => array(&$install_state), ->disableRedirect();
),
'no_redirect' => TRUE,
));
$form_builder = \Drupal::formBuilder(); $form_builder = \Drupal::formBuilder();
if ($install_state['interactive']) { if ($install_state['interactive']) {
$form = $form_builder->buildForm($form_id, $form_state); $form = $form_builder->buildForm($form_id, $form_state);
...@@ -827,7 +824,7 @@ function install_get_form($form_id, array &$install_state) { ...@@ -827,7 +824,7 @@ function install_get_form($form_id, array &$install_state) {
// values taken from the installation state. // values taken from the installation state.
$install_form_id = $form_builder->getFormId($form_id, $form_state); $install_form_id = $form_builder->getFormId($form_id, $form_state);
if (!empty($install_state['forms'][$install_form_id])) { if (!empty($install_state['forms'][$install_form_id])) {
$form_state->set('values', $install_state['forms'][$install_form_id]); $form_state->setValues($install_state['forms'][$install_form_id]);
} }
$form_builder->submitForm($form_id, $form_state); $form_builder->submitForm($form_id, $form_state);
......
...@@ -356,9 +356,8 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form ...@@ -356,9 +356,8 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
foreach ($this->getVisibilityConditions() as $condition_id => $condition) { foreach ($this->getVisibilityConditions() as $condition_id => $condition) {
// Allow the condition to validate the form. // Allow the condition to validate the form.
$condition_values = new FormState(array( $condition_values = (new FormState())
'values' => $form_state->getValue(array('visibility', $condition_id)), ->setValues($form_state->getValue(['visibility', $condition_id]));
));
$condition->validateConfigurationForm($form, $condition_values); $condition->validateConfigurationForm($form, $condition_values);
// Update the original form values. // Update the original form values.
$form_state->setValue(array('visibility', $condition_id), $condition_values['values']); $form_state->setValue(array('visibility', $condition_id), $condition_values['values']);
...@@ -389,9 +388,8 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s ...@@ -389,9 +388,8 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
$this->configuration['cache'] = $form_state->getValue('cache'); $this->configuration['cache'] = $form_state->getValue('cache');
foreach ($this->getVisibilityConditions() as $condition_id => $condition) { foreach ($this->getVisibilityConditions() as $condition_id => $condition) {
// Allow the condition to submit the form. // Allow the condition to submit the form.
$condition_values = new FormState(array( $condition_values = (new FormState())
'values' => $form_state->getValue(array('visibility', $condition_id)), ->setValues($form_state->getValue(['visibility', $condition_id]));
));
$condition->submitConfigurationForm($form, $condition_values); $condition->submitConfigurationForm($form, $condition_values);
// Update the original form values. // Update the original form values.
$form_state->setValue(array('visibility', $condition_id), $condition_values['values']); $form_state->setValue(array('visibility', $condition_id), $condition_values['values']);
......
...@@ -49,11 +49,7 @@ public function getForm(EntityInterface $entity, $operation = 'default', array $ ...@@ -49,11 +49,7 @@ public function getForm(EntityInterface $entity, $operation = 'default', array $
$form_object = $this->entityManager->getFormObject($entity->getEntityTypeId(), $operation); $form_object = $this->entityManager->getFormObject($entity->getEntityTypeId(), $operation);
$form_object->setEntity($entity); $form_object->setEntity($entity);
$form_state = new FormState($form_state_additions); $form_state = (new FormState())->setFormState($form_state_additions);
$form_state['build_info']['callback_object'] = $form_object;
$form_state['build_info']['base_form_id'] = $form_object->getBaseFormID();
$form_state['build_info'] += array('args' => array());
return $this->formBuilder->buildForm($form_object, $form_state); return $this->formBuilder->buildForm($form_object, $form_state);
} }
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
/** /**
* Provides an interface for a Form that has a base form ID. * Provides an interface for a Form that has a base form ID.
* *
* This will become the $form_state['build_info']['base_form_id'] used to * This will become the $form_state->getBaseInfo()['base_form_id'] used to
* generate the name of hook_form_BASE_FORM_ID_alter(). * generate the name of hook_form_BASE_FORM_ID_alter().
*/ */
interface BaseFormIdInterface extends FormInterface { interface BaseFormIdInterface extends FormInterface {
......
This diff is collapsed.
...@@ -42,7 +42,7 @@ public function getFormId($form_arg, FormStateInterface &$form_state); ...@@ -42,7 +42,7 @@ public function getFormId($form_arg, FormStateInterface &$form_state);
* function. For example, the node_edit form requires that a node object is * function. For example, the node_edit form requires that a node object is
* passed in here when it is called. These are available to implementations * passed in here when it is called. These are available to implementations
* of hook_form_alter() and hook_form_FORM_ID_alter() as the array * of hook_form_alter() and hook_form_FORM_ID_alter() as the array
* $form_state['build_info']['args']. * $form_state->getBuildInfo()['args'].
* *
* @return array * @return array
* The form array. * The form array.
...@@ -79,10 +79,10 @@ public function buildForm($form_id, FormStateInterface &$form_state); ...@@ -79,10 +79,10 @@ public function buildForm($form_id, FormStateInterface &$form_state);
* This is the key function for making multi-step forms advance from step to * This is the key function for making multi-step forms advance from step to
* step. It is called by self::processForm() when all user input processing, * step. It is called by self::processForm() when all user input processing,
* including calling validation and submission handlers, for the request is * including calling validation and submission handlers, for the request is
* finished. If a validate or submit handler set $form_state['rebuild'] to * finished. If a validate or submit handler set $form_state->isRebuilding()
* TRUE, and if other conditions don't preempt a rebuild from happening, then * to TRUE, and if other conditions don't preempt a rebuild from happening,
* this function is called to generate a new $form, the next step in the form * then this function is called to generate a new $form, the next step in the
* workflow, to be returned for rendering. * form workflow, to be returned for rendering.
* *
* Ajax form submissions are almost always multi-step workflows, so that is * Ajax form submissions are almost always multi-step workflows, so that is
* one common use-case during which form rebuilding occurs. See * one common use-case during which form rebuilding occurs. See
...@@ -98,7 +98,7 @@ public function buildForm($form_id, FormStateInterface &$form_state); ...@@ -98,7 +98,7 @@ public function buildForm($form_id, FormStateInterface &$form_state);
* (optional) A previously built $form. Used to retain the #build_id and * (optional) A previously built $form. Used to retain the #build_id and
* #action properties in Ajax callbacks and similar partial form rebuilds. * #action properties in Ajax callbacks and similar partial form rebuilds.
* The only properties copied from $old_form are the ones which both exist * The only properties copied from $old_form are the ones which both exist
* in $old_form and for which $form_state['rebuild_info']['copy'][PROPERTY] * in $old_form and for which $form_state->getRebuildInfo()['copy'][PROPERTY]
* is TRUE. If $old_form is not passed, the entire $form is rebuilt freshly. * is TRUE. If $old_form is not passed, the entire $form is rebuilt freshly.
* 'rebuild_info' needs to be a separate top-level property next to * 'rebuild_info' needs to be a separate top-level property next to
* 'build_info', since the contained data must not be cached. * 'build_info', since the contained data must not be cached.
...@@ -148,8 +148,8 @@ public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form ...@@ -148,8 +148,8 @@ public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form
* @endcode * @endcode
* would be called via self::submitForm() as follows: * would be called via self::submitForm() as follows:
* @code * @code
* $form_state->set('values', $my_form_values); * $form_state->setValues($my_form_values);
* $form_state['build_info']['args'] = array(&$object); * $form_state->addBuildInfo('args', [&$object]);
* drupal_form_submit('mymodule_form', $form_state); * drupal_form_submit('mymodule_form', $form_state);
* @endcode * @endcode
* For example: * For example:
...@@ -161,7 +161,7 @@ public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form ...@@ -161,7 +161,7 @@ public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form
* $values['pass']['pass1'] = 'password'; * $values['pass']['pass1'] = 'password';
* $values['pass']['pass2'] = 'password'; * $values['pass']['pass2'] = 'password';
* $values['op'] = t('Create new account'); * $values['op'] = t('Create new account');
* $form_state->set('values', $values); * $form_state->setValues($values);
* drupal_form_submit('user_register_form', $form_state); * drupal_form_submit('user_register_form', $form_state);
* @endcode * @endcode
*/ */
...@@ -293,8 +293,8 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state); ...@@ -293,8 +293,8 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state);
* the next submission needs to be processed, a multi-step workflow is * the next submission needs to be processed, a multi-step workflow is
* needed. This is most commonly implemented with a submit handler setting * needed. This is most commonly implemented with a submit handler setting
* persistent data within $form_state based on *validated* values in * persistent data within $form_state based on *validated* values in
* $form_state->getValues() and setting $form_state['rebuild']. The form * $form_state->getValues() and checking $form_state->isRebuilding(). The
* building functions must then be implemented to use the $form_state data * form building functions must then be implemented to use the $form_state
* to rebuild the form with the structure appropriate for the new state. * to rebuild the form with the structure appropriate for the new state.
* - Where user input must affect the rendering of the form without affecting * - Where user input must affect the rendering of the form without affecting
* its structure, the necessary conditional rendering logic should reside * its structure, the necessary conditional rendering logic should reside
......
...@@ -95,8 +95,9 @@ protected function loadCachedFormState($form_build_id, FormStateInterface $form_ ...@@ -95,8 +95,9 @@ protected function loadCachedFormState($form_build_id, FormStateInterface $form_
// If the original form is contained in include files, load the files. // If the original form is contained in include files, load the files.
// @see \Drupal\Core\Form\FormStateInterface::loadInclude() // @see \Drupal\Core\Form\FormStateInterface::loadInclude()
$form_state['build_info'] += array('files' => array()); $build_info = $form_state->getBuildInfo();
foreach ($form_state['build_info']['files'] as $file) { $build_info += ['files' => []];
foreach ($build_info['files'] as $file) {
if (is_array($file)) { if (is_array($file)) {
$file += array('type' => 'inc', 'name' => $file['module']); $file += array('type' => 'inc', 'name' => $file['module']);
$this->moduleHandler->loadInclude($file['module'], $file['type'], $file['name']); $this->moduleHandler->loadInclude($file['module'], $file['type'], $file['name']);
...@@ -109,9 +110,10 @@ protected function loadCachedFormState($form_build_id, FormStateInterface $form_ ...@@ -109,9 +110,10 @@ protected function loadCachedFormState($form_build_id, FormStateInterface $form_
// for this request. // for this request.
// @todo Ensure we are not storing an excessively large string list // @todo Ensure we are not storing an excessively large string list
// in: https://www.drupal.org/node/2295823 // in: https://www.drupal.org/node/2295823
$form_state['build_info'] += array('safe_strings' => array()); $build_info += ['safe_strings' => []];
SafeMarkup::setMultiple($form_state['build_info']['safe_strings']); SafeMarkup::setMultiple($build_info['safe_strings']);
unset($form_state['build_info']['safe_strings']); unset($build_info['safe_strings']);
$form_state->setBuildInfo($build_info);
} }
} }
......
This diff is collapsed.
...@@ -95,18 +95,6 @@ public function getCacheableArray(); ...@@ -95,18 +95,6 @@ public function getCacheableArray();
*/ */
public function setFormState(array $form_state_additions); public function setFormState(array $form_state_additions);
/**
* Sets a value to an arbitrary property if it does not exist yet.
*
* @param string $property
* The property to use for the value.
* @param mixed $value
* The data to store.
*
* @return $this
*/
public function setIfNotExists($property, $value);
/** /**
* Sets a response for this form. * Sets a response for this form.
* *
...@@ -120,6 +108,17 @@ public function setIfNotExists($property, $value); ...@@ -120,6 +108,17 @@ public function setIfNotExists($property, $value);
*/ */
public function setResponse(Response $response); public function setResponse(Response $response);
/**
* Gets a response for this form.
*
* If a response is set, it will be used during processing and returned
* directly. The form will not be rebuilt or redirected.
*
* @return \Symfony\Component\HttpFoundation\Response|null
* The response to return, or NULL.
*/
public function getResponse();
/** /**
* Sets the redirect for the form. * Sets the redirect for the form.
* *
...@@ -164,11 +163,32 @@ public function setRedirectUrl(Url $url); ...@@ -164,11 +163,32 @@ public function setRedirectUrl(Url $url);
*/ */
public function getRedirect(); public function getRedirect();
/**
* Sets the entire set of arbitrary data.
*
* @param array $storage
* The entire set of arbitrary data to store for this form.
*
* @return $this
*/
public function setStorage(array $storage);
/**
* Returns the entire set of arbitrary data.
*
* @return array
* The entire set of arbitrary data to store for this form.
*/
public function &getStorage();
/** /**
* Gets any arbitrary property. * Gets any arbitrary property.
* *
* @param string $property * @param string|array $property
* The property to retrieve. * Properties are often stored as multi-dimensional associative arrays. If
* $property is a string, it will return $storage[$property]. If $property
* is an array, each element of the array will be used as a nested key. If
* $property = ['foo', 'bar'] it will return $storage['foo']['bar'].
* *
* @return mixed * @return mixed
* A reference to the value for that property, or NULL if the property does * A reference to the value for that property, or NULL if the property does
...@@ -179,8 +199,12 @@ public function &get($property); ...@@ -179,8 +199,12 @@ public function &get($property);
/** /**
* Sets a value to an arbitrary property. * Sets a value to an arbitrary property.
* *
* @param string $property * @param string|array $property
* The property to use for the value. * Properties are often stored as multi-dimensional associative arrays. If
* $property is a string, it will use $storage[$property] = $value. If
* $property is an array, each element of the array will be used as a nested
* key. If $property = ['foo', 'bar'] it will use
* $storage['foo']['bar'] = $value.
* @param mixed $value * @param mixed $value
* The value to set. * The value to set.
* *
...@@ -189,11 +213,39 @@ public function &get($property); ...@@ -189,11 +213,39 @@ public function &get($property);
public function set($property, $value); public function set($property, $value);
/** /**
* Determines if an arbitrary property is present.
*
* @param string $property * @param string $property
* The property to use for the value. * Properties are often stored as multi-dimensional associative arrays. If
* $property is a string, it will return isset($storage[$property]). If
* $property is an array, each element of the array will be used as a nested
* key. If $property = ['foo', 'bar'] it will return
* isset($storage['foo']['bar']).
*/ */
public function has($property); public function has($property);
/**
* Sets the build info for the form.
*
* @param array $build_info
* An array of build info.
*
* @return $this
*
* @see \Drupal\Core\Form\FormState::$build_info
*/
public function setBuildInfo(array $build_info);
/**
* Returns the build info for the form.
*
* @return array
* An array of build info.
*
* @see \Drupal\Core\Form\FormState::$build_info
*/
public function getBuildInfo();
/** /**
* Adds a value to the build info. * Adds a value to the build info.
* *
...@@ -252,6 +304,19 @@ public function &getValues(); ...@@ -252,6 +304,19 @@ public function &getValues();
*/ */
public function &getValue($key, $default = NULL); public function &getValue($key, $default = NULL);
/**
* Sets the submitted form values.
*
* This should be avoided, since these values have been validated already. Use
* self::setUserInput() instead.
*
* @param array $values
* The multi-dimensional associative array of form values.
*
* @return $this
*/
public function setValues(array $values);
/** /**
* Sets the submitted form value for a specific key. * Sets the submitted form value for a specific key.
* *
...@@ -335,7 +400,7 @@ public function isValueEmpty($key); ...@@ -335,7 +400,7 @@ public function isValueEmpty($key);
* *
* @return $this * @return $this
*/ */
public function setValueForElement($element, $value); public function setValueForElement(array $element, $value);
/** /**
* Determines if any forms have any errors. * Determines if any forms have any errors.
...@@ -448,7 +513,7 @@ public function setErrorByName($name, $message = ''); ...@@ -448,7 +513,7 @@ public function setErrorByName($name, $message = '');
* *
* @return $this * @return $this
*/ */
public function setError(&$element, $message = ''); public function setError(array &$element, $message = '');
/** /**
* Clears all errors against all form elements made by self::setErrorByName(). * Clears all errors against all form elements made by self::setErrorByName().
...@@ -475,7 +540,7 @@ public function getErrors(); ...@@ -475,7 +540,7 @@ public function getErrors();
* @return string|null * @return string|null
* Either the error message for this element or NULL if there are no errors. * Either the error message for this element or NULL if there are no errors.
*/ */
public function getError($element); public function getError(array $element);
/** /**
* Sets the form to be rebuilt after processing. * Sets the form to be rebuilt after processing.
...@@ -487,6 +552,14 @@ public function getError($element); ...@@ -487,6 +552,14 @@ public function getError($element);
*/ */
public function setRebuild($rebuild = TRUE); public function setRebuild($rebuild = TRUE);
/**
* Determines if the form should be rebuilt after processing.
*
* @return bool
* TRUE if the form should be rebuilt, FALSE otherwise.
*/
public function isRebuilding();
/** /**
* Converts support notations for a form callback to a valid callable. * Converts support notations for a form callback to a valid callable.
* *
...@@ -509,4 +582,409 @@ public function prepareCallback($callback); ...@@ -509,4 +582,409 @@ public function prepareCallback($callback);
*/ */
public function getFormObject(); public function getFormObject();
/**
* Sets the form object that is responsible for building this form.
*
* @param \Drupal\Core\Form\FormInterface $form_object
* The form object.
*
* @return $this
*/
public function setFormObject(FormInterface $form_object);
/**
* Sets this form to always be processed.
*
* This should only be used on RESTful GET forms that do NOT write data, as
* this could lead to security issues. It is useful so that searches do not
* need to have a form_id in their query arguments to trigger the search.
*
* @param bool $always_process
* TRUE if the form should always be processed, FALSE otherwise.
*
* @return $this
*/
public function setAlwaysProcess($always_process = TRUE);
/**
* Determines if this form should always be processed.
*
* @return bool
* TRUE if the form should always be processed, FALSE otherwise.
*/
public function getAlwaysProcess();
/**
* Stores the submit and button elements for the form.
*
* @param array $buttons
* The submit and button elements.
*
* @return $this
*/
public function setButtons(array $buttons);
/**
* Returns the submit and button elements for the form.
*
* @return array
* The submit and button elements.
*/
public function getButtons();
/**
* Sets this form to be cached.
*
* @param bool $cache
* TRUE if the form should be cached, FALSE otherwise.
*
* @return $this
*/
public function setCached($cache = TRUE);
/**
* Determines if the form should be cached.
*
* @return bool
* TRUE if the form should be cached, FALSE otherwise.
*/
public function isCached();
/**
* Prevents the form from being cached.
*
* @return $this
*/
public function disableCache();
/**
* Sets that the form was submitted and has been processed and executed.
*
* @return $this
*/
public function setExecuted();
/**
* Determines if the form was submitted and has been processed and executed.
*
* @return bool
* TRUE if the form was submitted and has been processed and executed.
*/
public function isExecuted();
/**
* Sets references to details elements to render them within vertical tabs.
*
* @param array $groups
* References to details elements to render them within vertical tabs.
*
* @return $this
*/
public function setGroups(array $groups);
/**
* Returns references to details elements to render them within vertical tabs.
*
* @return array
*/
public function getGroups();
/**
* Sets that this form has a file element.
*
* @param bool $has_file_element
* Whether this form has a file element.
*
* @return $this
*/
public function setHasFileElement($has_file_element = TRUE);
/**
* Returns whether this form has a file element.
*
* @return bool
* Whether this form has a file element.
*/
public function hasFileElement();
/**
* Sets the limited validation error sections.
*
* @param array|null $limit_validation_errors
* The limited validation error sections.
*
* @return $this
*
* @see \Drupal\Core\Form\FormState::$limit_validation_errors