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) {
function install_get_form($form_id, array &$install_state) {
// Ensure the form will not redirect, since install_run_tasks() uses a custom
// redirection logic.
$form_state = new FormState(array(
'build_info' => array(
'args' => array(&$install_state),
),
'no_redirect' => TRUE,
));
$form_state = (new FormState())
->addBuildInfo('args', [&$install_state])
->disableRedirect();
$form_builder = \Drupal::formBuilder();
if ($install_state['interactive']) {
$form = $form_builder->buildForm($form_id, $form_state);
......@@ -827,7 +824,7 @@ function install_get_form($form_id, array &$install_state) {
// values taken from the installation state.
$install_form_id = $form_builder->getFormId($form_id, $form_state);
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);
......
......@@ -356,9 +356,8 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
foreach ($this->getVisibilityConditions() as $condition_id => $condition) {
// Allow the condition to validate the form.
$condition_values = new FormState(array(
'values' => $form_state->getValue(array('visibility', $condition_id)),
));
$condition_values = (new FormState())
->setValues($form_state->getValue(['visibility', $condition_id]));
$condition->validateConfigurationForm($form, $condition_values);
// Update the original form values.
$form_state->setValue(array('visibility', $condition_id), $condition_values['values']);
......@@ -389,9 +388,8 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
$this->configuration['cache'] = $form_state->getValue('cache');
foreach ($this->getVisibilityConditions() as $condition_id => $condition) {
// Allow the condition to submit the form.
$condition_values = new FormState(array(
'values' => $form_state->getValue(array('visibility', $condition_id)),
));
$condition_values = (new FormState())
->setValues($form_state->getValue(['visibility', $condition_id]));
$condition->submitConfigurationForm($form, $condition_values);
// Update the original form values.
$form_state->setValue(array('visibility', $condition_id), $condition_values['values']);
......
......@@ -49,11 +49,7 @@ public function getForm(EntityInterface $entity, $operation = 'default', array $
$form_object = $this->entityManager->getFormObject($entity->getEntityTypeId(), $operation);
$form_object->setEntity($entity);
$form_state = new FormState($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());
$form_state = (new FormState())->setFormState($form_state_additions);
return $this->formBuilder->buildForm($form_object, $form_state);
}
......
......@@ -10,7 +10,7 @@
/**
* 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().
*/
interface BaseFormIdInterface extends FormInterface {
......
This diff is collapsed.
......@@ -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
* 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
* $form_state['build_info']['args'].
* $form_state->getBuildInfo()['args'].
*
* @return array
* The form array.
......@@ -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
* step. It is called by self::processForm() when all user input processing,
* including calling validation and submission handlers, for the request is
* finished. If a validate or submit handler set $form_state['rebuild'] to
* TRUE, and if other conditions don't preempt a rebuild from happening, then
* this function is called to generate a new $form, the next step in the form
* workflow, to be returned for rendering.
* finished. If a validate or submit handler set $form_state->isRebuilding()
* to TRUE, and if other conditions don't preempt a rebuild from happening,
* then this function is called to generate a new $form, the next step in the
* form workflow, to be returned for rendering.
*
* Ajax form submissions are almost always multi-step workflows, so that is
* one common use-case during which form rebuilding occurs. See
......@@ -98,7 +98,7 @@ public function buildForm($form_id, FormStateInterface &$form_state);
* (optional) A previously built $form. Used to retain the #build_id and
* #action properties in Ajax callbacks and similar partial form rebuilds.
* 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.
* 'rebuild_info' needs to be a separate top-level property next to
* 'build_info', since the contained data must not be cached.
......@@ -148,8 +148,8 @@ public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form
* @endcode
* would be called via self::submitForm() as follows:
* @code
* $form_state->set('values', $my_form_values);
* $form_state['build_info']['args'] = array(&$object);
* $form_state->setValues($my_form_values);
* $form_state->addBuildInfo('args', [&$object]);
* drupal_form_submit('mymodule_form', $form_state);
* @endcode
* For example:
......@@ -161,7 +161,7 @@ public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form
* $values['pass']['pass1'] = 'password';
* $values['pass']['pass2'] = 'password';
* $values['op'] = t('Create new account');
* $form_state->set('values', $values);
* $form_state->setValues($values);
* drupal_form_submit('user_register_form', $form_state);
* @endcode
*/
......@@ -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
* needed. This is most commonly implemented with a submit handler setting
* persistent data within $form_state based on *validated* values in
* $form_state->getValues() and setting $form_state['rebuild']. The form
* building functions must then be implemented to use the $form_state data
* $form_state->getValues() and checking $form_state->isRebuilding(). The
* form building functions must then be implemented to use the $form_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
* its structure, the necessary conditional rendering logic should reside
......
......@@ -95,8 +95,9 @@ protected function loadCachedFormState($form_build_id, FormStateInterface $form_
// If the original form is contained in include files, load the files.
// @see \Drupal\Core\Form\FormStateInterface::loadInclude()
$form_state['build_info'] += array('files' => array());
foreach ($form_state['build_info']['files'] as $file) {
$build_info = $form_state->getBuildInfo();
$build_info += ['files' => []];
foreach ($build_info['files'] as $file) {
if (is_array($file)) {
$file += array('type' => 'inc', 'name' => $file['module']);
$this->moduleHandler->loadInclude($file['module'], $file['type'], $file['name']);
......@@ -109,9 +110,10 @@ protected function loadCachedFormState($form_build_id, FormStateInterface $form_
// for this request.
// @todo Ensure we are not storing an excessively large string list
// in: https://www.drupal.org/node/2295823
$form_state['build_info'] += array('safe_strings' => array());
SafeMarkup::setMultiple($form_state['build_info']['safe_strings']);
unset($form_state['build_info']['safe_strings']);
$build_info += ['safe_strings' => []];
SafeMarkup::setMultiple($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();
*/
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.
*
......@@ -120,6 +108,17 @@ public function setIfNotExists($property, $value);
*/
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.
*
......@@ -164,11 +163,32 @@ public function setRedirectUrl(Url $url);
*/
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.
*
* @param string $property
* The property to retrieve.
* @param string|array $property
* 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
* A reference to the value for that property, or NULL if the property does
......@@ -179,8 +199,12 @@ public function &get($property);
/**
* Sets a value to an arbitrary property.
*
* @param string $property
* The property to use for the value.
* @param string|array $property
* 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
* The value to set.
*
......@@ -189,11 +213,39 @@ public function &get($property);
public function set($property, $value);
/**
* Determines if an arbitrary property is present.
*
* @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);
/**
* 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.
*
......@@ -252,6 +304,19 @@ public function &getValues();
*/
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.
*
......@@ -335,7 +400,7 @@ public function isValueEmpty($key);
*
* @return $this
*/
public function setValueForElement($element, $value);
public function setValueForElement(array $element, $value);
/**
* Determines if any forms have any errors.
......@@ -448,7 +513,7 @@ public function setErrorByName($name, $message = '');
*
* @return $this
*/
public function setError(&$element, $message = '');
public function setError(array &$element, $message = '');
/**
* Clears all errors against all form elements made by self::setErrorByName().
......@@ -475,7 +540,7 @@ public function getErrors();
* @return string|null
* 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.
......@@ -487,6 +552,14 @@ public function getError($element);
*/
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.
*
......@@ -509,4 +582,409 @@ public function prepareCallback($callback);
*/
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
*/
public function setLimitValidationErrors($limit_validation_errors);
/**
* Retrieves the limited validation error sections.
*
* @return array|null
* The limited validation error sections.
*
* @see \Drupal\Core\Form\FormState::$limit_validation_errors
*/
public function getLimitValidationErrors();
/**
* Sets the HTTP form method.
*
* @param string $method
* The HTTP form method.
*
* @see \Drupal\Core\Form\FormState::$method
*
* @return $this
*/
public function setMethod($method);
/**
* Returns the HTTP form method.
*
* @param string
* The HTTP form method.
*
* @return bool
* TRUE if the HTTP form method matches.
*
* @see \Drupal\Core\Form\FormState::$method
*/
public function isMethodType($method_type);
/**
* Enforces that validation is run.
*
* @param bool $must_validate
* If TRUE, validation will always be run.
*
* @return $this
*/
public function setValidationEnforced($must_validate = TRUE);
/**
* Checks if validation is enforced.
*
* @return bool
* If TRUE, validation will always be run.
*/
public function isValidationEnforced();
/**
* Prevents the form from redirecting.
*
* @param bool $no_redirect
* If TRUE, the form will not redirect.
*
* @return $this
*/
public function disableRedirect($no_redirect = TRUE);
/**
* Determines if redirecting has been prevented.
*
* @return bool
* If TRUE, the form will not redirect.
*/
public function isRedirectDisabled();
/**
* Sets that the form should process input.
*
* @param bool $process_input
* If TRUE, the form input will be processed.
*
* @return $this
*/
public function setProcessInput($process_input = TRUE);
/**
* Determines if the form input will be processed.
*
* @return bool
* If TRUE, the form input will be processed.
*/
public function isProcessingInput();
/**
* Sets that this form was submitted programmatically.
*
* @param bool $programmed
* If TRUE, the form was submitted programmatically.
*
* @return $this
*/
public function setProgrammed($programmed = TRUE);
/**
* Returns if this form was submitted programmatically.
*
* @return bool
* If TRUE, the form was submitted programmatically.
*/
public function isProgrammed();
/**
* Sets if this form submission should bypass #access.
*
* @param bool $programmed_bypass_access_check
* If TRUE, programmatic form submissions are processed without taking
* #access into account.
*
* @return $this
*
* @see \Drupal\Core\Form\FormState::$programmed_bypass_access_check
*/
public function setProgrammedBypassAccessCheck($programmed_bypass_access_check = TRUE);