Commit fc5e111a authored by Dries's avatar Dries

Issue #2225353 by tim.plunkett: Convert to an object and provide methods like setError().

parent 4d07e60a
......@@ -5,6 +5,8 @@
* Functions for use with Drupal's Ajax framework.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* @defgroup ajax Ajax framework
* @{
......@@ -64,7 +66,7 @@
* return \Drupal::formBuilder()->getForm('ajax_example_simplest');
* }
*
* function ajax_example_simplest($form, &$form_state) {
* function ajax_example_simplest($form, FormStateInterface $form_state) {
* $form = array();
* $form['changethis'] = array(
* '#type' => 'select',
......@@ -236,7 +238,7 @@
*
* @see ajax_pre_render_element()
*/
function ajax_process_form($element, &$form_state) {
function ajax_process_form($element, FormStateInterface $form_state) {
$element = ajax_pre_render_element($element);
if (!empty($element['#ajax_processed'])) {
$form_state['cache'] = TRUE;
......
......@@ -17,6 +17,7 @@
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Timer;
use Drupal\Core\Batch\Percentage;
use Drupal\Core\Form\FormState;
use Drupal\Core\Page\DefaultHtmlPageRenderer;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
......@@ -442,6 +443,9 @@ function _batch_finished() {
}
// Determine the target path to redirect to.
if (!isset($_batch['form_state'])) {
$_batch['form_state'] = new FormState();
}
if (!isset($_batch['form_state']['redirect'])) {
if (isset($_batch['redirect'])) {
$_batch['form_state']['redirect'] = $_batch['redirect'];
......
This diff is collapsed.
......@@ -7,6 +7,7 @@
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\Install\TaskException;
use Drupal\Core\Form\FormState;
use Drupal\Core\Installer\Exception\AlreadyInstalledException;
use Drupal\Core\Installer\Exception\InstallerException;
use Drupal\Core\Installer\Exception\NoProfilesException;
......@@ -157,7 +158,7 @@ function install_drupal($settings = array()) {
* submission is for, and the values are used as the $form_state['values']
* array that is passed on to the form submission via drupal_form_submit()).
*
* @see drupal_form_submit()
* @see \Drupal\Core\Form\FormBuilderInterface::submitForm()
*/
function install_state_defaults() {
$defaults = array(
......@@ -804,12 +805,12 @@ 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 = array(
$form_state = new FormState(array(
'build_info' => array(
'args' => array(&$install_state),
),
'no_redirect' => TRUE,
);
));
$form_builder = \Drupal::formBuilder();
if ($install_state['interactive']) {
$form = $form_builder->buildForm($form_id, $form_state);
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
......@@ -49,7 +50,7 @@ public function setConfiguration(array $configuration) {
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, array &$form_state) {
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Condition;
use Drupal\Core\Executable\ExecutablePluginBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a basis for fulfilling contexts for condition plugins.
......@@ -39,7 +40,7 @@ public function isNegated() {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, array &$form_state) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['negate'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Negate the condition.'),
......@@ -51,13 +52,13 @@ public function buildConfigurationForm(array $form, array &$form_state) {
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, array &$form_state) {
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['negate'] = $form_state['values']['negate'];
}
......
......@@ -11,6 +11,7 @@
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines a class to build a draggable listing of configuration entities.
......@@ -102,7 +103,7 @@ public function render() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
public function buildForm(array $form, FormStateInterface $form_state) {
$form[$this->entitiesKey] = array(
'#type' => 'table',
'#header' => $this->buildHeader(),
......@@ -138,14 +139,14 @@ public function buildForm(array $form, array &$form_state) {
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
public function validateForm(array &$form, FormStateInterface $form_state) {
// No validation.
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state['values'][$this->entitiesKey] as $id => $value) {
if (isset($this->entities[$id]) && $this->entities[$id]->get($this->weightKey) != $value['weight']) {
// Save entity only when its weight was changed.
......
......@@ -9,6 +9,7 @@
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -66,7 +67,7 @@ public function getContentResult(Request $request) {
// Add the form and form_state to trick the getArguments method of the
// controller resolver.
$form_state = array();
$form_state = new FormState();
$request->attributes->set('form', array());
$request->attributes->set('form_state', $form_state);
$args = $this->controllerResolver->getArguments($request, array($form_object, 'buildForm'));
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Display;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginDependencyTrait;
use Drupal\Core\Session\AccountInterface;
......@@ -100,7 +101,7 @@ public function calculateDependencies() {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, array &$form_state) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this->t('Label'),
......@@ -114,13 +115,13 @@ public function buildConfigurationForm(array $form, array &$form_state) {
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, array &$form_state) {
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['label'] = $form_state['values']['label'];
}
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Form\ConfirmFormHelper;
use Drupal\Core\Form\ConfirmFormInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a generic base class for an entity-based confirmation form.
......@@ -53,7 +54,7 @@ public function getFormName() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['#title'] = $this->getQuestion();
......@@ -72,7 +73,7 @@ public function buildForm(array $form, array &$form_state) {
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
public function form(array $form, FormStateInterface $form_state) {
// Do not attach fields to the confirm form.
return $form;
}
......@@ -80,7 +81,7 @@ public function form(array $form, array &$form_state) {
/**
* {@inheritdoc}
*/
protected function actions(array $form, array &$form_state) {
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']);
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity\Entity\EntityFormDisplay;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -47,7 +48,7 @@ public static function create(ContainerInterface $container) {
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$this->getFormDisplay($form_state)->buildForm($this->entity, $form, $form_state);
return $form;
......@@ -56,7 +57,7 @@ public function form(array $form, array &$form_state) {
/**
* {@inheritdoc}
*/
public function validate(array $form, array &$form_state) {
public function validate(array $form, FormStateInterface $form_state) {
$this->updateFormLangcode($form_state);
$entity = $this->buildEntity($form, $form_state);
$this->getFormDisplay($form_state)->validateFormValues($entity, $form, $form_state);
......@@ -70,7 +71,7 @@ public function validate(array $form, array &$form_state) {
/**
* Initialize the form state and the entity before the first form build.
*/
protected function init(array &$form_state) {
protected function init(FormStateInterface $form_state) {
// Ensure we act on the translation object corresponding to the current form
// language.
$langcode = $this->getFormLangcode($form_state);
......@@ -85,7 +86,7 @@ protected function init(array &$form_state) {
/**
* {@inheritdoc}
*/
public function getFormLangcode(array &$form_state) {
public function getFormLangcode(FormStateInterface $form_state) {
if (empty($form_state['langcode'])) {
// Imply a 'view' operation to ensure users edit entities in the same
// language they are displayed. This allows to keep contextual editing
......@@ -98,14 +99,14 @@ public function getFormLangcode(array &$form_state) {
/**
* {@inheritdoc}
*/
public function isDefaultFormLangcode(array $form_state) {
public function isDefaultFormLangcode(FormStateInterface $form_state) {
return $this->getFormLangcode($form_state) == $this->entity->getUntranslated()->language()->id;
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, array &$form_state) {
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
// First, extract values from widgets.
$extracted = $this->getFormDisplay($form_state)->extractFormValues($entity, $form, $form_state);
......@@ -122,14 +123,14 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form,
/**
* {@inheritdoc}
*/
public function getFormDisplay(array $form_state) {
public function getFormDisplay(FormStateInterface $form_state) {
return isset($form_state['form_display']) ? $form_state['form_display'] : NULL;
}
/**
* {@inheritdoc}
*/
public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state) {
public function setFormDisplay(EntityFormDisplayInterface $form_display, FormStateInterface $form_state) {
$form_state['form_display'] = $form_display;
return $this;
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines a common interface for content entity form classes.
......@@ -17,13 +18,13 @@
/**
* Returns the form display.
*
* @param array $form_state
* An associative array containing the current state of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface.
* The current form display.
*/
public function getFormDisplay(array $form_state);
public function getFormDisplay(FormStateInterface $form_state);
/**
* Sets the form display.
......@@ -33,9 +34,9 @@ public function getFormDisplay(array $form_state);
*
* @param \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display
* The form display that the current form operates with.
* @param array $form_state
* An associative array containing the current state of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state);
public function setFormDisplay(EntityFormDisplayInterface $form_display, FormStateInterface $form_state);
}
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Entity\Display;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a common interface for entity form displays.
......@@ -93,10 +94,10 @@
* $form_state['values']. If not specified, $form['#parents'] is set to an
* empty array, which results in field values located at the top-level of
* $form_state['values'].
* @param array $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function buildForm(ContentEntityInterface $entity, array &$form, array &$form_state);
public function buildForm(ContentEntityInterface $entity, array &$form, FormStateInterface $form_state);
/**
* Validates submitted widget values and sets the corresponding form errors.
......@@ -122,10 +123,10 @@ public function buildForm(ContentEntityInterface $entity, array &$form, array &$
* @param array $form
* The form structure where field elements are attached to. This might be a
* full form structure, or a sub-element of a larger form.
* @param array $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function validateFormValues(ContentEntityInterface $entity, array &$form, array &$form_state);
public function validateFormValues(ContentEntityInterface $entity, array &$form, FormStateInterface $form_state);