Issue #1913084 by tim.plunkett: Introduce a Form interface for building, validating, and submitting forms.
parent
95f1d8c8c2
commit
bd8f0f9b71
|
@ -768,7 +768,7 @@ function drupal_retrieve_form($form_id, &$form_state) {
|
||||||
$callback = $form_state['build_info']['callback'];
|
$callback = $form_state['build_info']['callback'];
|
||||||
}
|
}
|
||||||
elseif (!empty($form_state['build_info']['callback_object'])) {
|
elseif (!empty($form_state['build_info']['callback_object'])) {
|
||||||
$callback = array($form_state['build_info']['callback_object'], 'build');
|
$callback = array($form_state['build_info']['callback_object'], 'buildForm');
|
||||||
}
|
}
|
||||||
|
|
||||||
// We first check to see if there is a valid form builder callback defined.
|
// We first check to see if there is a valid form builder callback defined.
|
||||||
|
@ -1078,7 +1078,7 @@ function drupal_prepare_form($form_id, &$form, &$form_state) {
|
||||||
// Ensure that modules can rely on #validate being set.
|
// Ensure that modules can rely on #validate being set.
|
||||||
$form['#validate'] = array();
|
$form['#validate'] = array();
|
||||||
if (isset($form_state['build_info']['callback_object'])) {
|
if (isset($form_state['build_info']['callback_object'])) {
|
||||||
$form['#validate'][] = array($form_state['build_info']['callback_object'], 'validate');
|
$form['#validate'][] = array($form_state['build_info']['callback_object'], 'validateForm');
|
||||||
}
|
}
|
||||||
// Check for a handler specific to $form_id.
|
// Check for a handler specific to $form_id.
|
||||||
elseif (function_exists($form_id . '_validate')) {
|
elseif (function_exists($form_id . '_validate')) {
|
||||||
|
@ -1095,7 +1095,7 @@ function drupal_prepare_form($form_id, &$form, &$form_state) {
|
||||||
// Ensure that modules can rely on #submit being set.
|
// Ensure that modules can rely on #submit being set.
|
||||||
$form['#submit'] = array();
|
$form['#submit'] = array();
|
||||||
if (isset($form_state['build_info']['callback_object'])) {
|
if (isset($form_state['build_info']['callback_object'])) {
|
||||||
$form['#submit'][] = array($form_state['build_info']['callback_object'], 'submit');
|
$form['#submit'][] = array($form_state['build_info']['callback_object'], 'submitForm');
|
||||||
}
|
}
|
||||||
// Check for a handler specific to $form_id.
|
// Check for a handler specific to $form_id.
|
||||||
elseif (function_exists($form_id . '_submit')) {
|
elseif (function_exists($form_id . '_submit')) {
|
||||||
|
|
|
@ -31,7 +31,7 @@ interface FormInterface {
|
||||||
* @return array
|
* @return array
|
||||||
* The form structure.
|
* The form structure.
|
||||||
*/
|
*/
|
||||||
public function build(array $form, array &$form_state);
|
public function buildForm(array $form, array &$form_state);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Form validation handler.
|
* Form validation handler.
|
||||||
|
@ -41,7 +41,7 @@ interface FormInterface {
|
||||||
* @param array $form_state
|
* @param array $form_state
|
||||||
* An associative array containing the current state of the form.
|
* An associative array containing the current state of the form.
|
||||||
*/
|
*/
|
||||||
public function validate(array &$form, array &$form_state);
|
public function validateForm(array &$form, array &$form_state);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Form submission handler.
|
* Form submission handler.
|
||||||
|
@ -51,6 +51,6 @@ interface FormInterface {
|
||||||
* @param array $form_state
|
* @param array $form_state
|
||||||
* An associative array containing the current state of the form.
|
* An associative array containing the current state of the form.
|
||||||
*/
|
*/
|
||||||
public function submit(array &$form, array &$form_state);
|
public function submitForm(array &$form, array &$form_state);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,11 +100,11 @@ class BlockListController extends ConfigEntityListController implements FormInte
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::build().
|
* Implements \Drupal\Core\Form\FormInterface::buildForm().
|
||||||
*
|
*
|
||||||
* Form constructor for the main block administration form.
|
* Form constructor for the main block administration form.
|
||||||
*/
|
*/
|
||||||
public function build(array $form, array &$form_state) {
|
public function buildForm(array $form, array &$form_state) {
|
||||||
$entities = $this->load();
|
$entities = $this->load();
|
||||||
$form['#attached']['css'][] = drupal_get_path('module', 'block') . '/block.admin.css';
|
$form['#attached']['css'][] = drupal_get_path('module', 'block') . '/block.admin.css';
|
||||||
$form['#attached']['library'][] = array('system', 'drupal.tableheader');
|
$form['#attached']['library'][] = array('system', 'drupal.tableheader');
|
||||||
|
@ -190,18 +190,18 @@ class BlockListController extends ConfigEntityListController implements FormInte
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::validate().
|
* Implements \Drupal\Core\Form\FormInterface::validateForm().
|
||||||
*/
|
*/
|
||||||
public function validate(array &$form, array &$form_state) {
|
public function validateForm(array &$form, array &$form_state) {
|
||||||
// No validation.
|
// No validation.
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::submit().
|
* Implements \Drupal\Core\Form\FormInterface::submitForm().
|
||||||
*
|
*
|
||||||
* Form submission handler for the main block administration form.
|
* Form submission handler for the main block administration form.
|
||||||
*/
|
*/
|
||||||
public function submit(array &$form, array &$form_state) {
|
public function submitForm(array &$form, array &$form_state) {
|
||||||
$entities = entity_load_multiple('block', array_keys($form_state['values']['blocks']));
|
$entities = entity_load_multiple('block', array_keys($form_state['values']['blocks']));
|
||||||
foreach ($entities as $entity_id => $entity) {
|
foreach ($entities as $entity_id => $entity) {
|
||||||
$entity->set('weight', $form_state['values']['blocks'][$entity_id]['weight']);
|
$entity->set('weight', $form_state['values']['blocks'][$entity_id]['weight']);
|
||||||
|
|
|
@ -39,9 +39,9 @@ class DisplayOverview extends OverviewBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::build().
|
* Implements \Drupal\Core\Form\FormInterface::buildForm().
|
||||||
*/
|
*/
|
||||||
public function build(array $form, array &$form_state) {
|
public function buildForm(array $form, array &$form_state) {
|
||||||
// Gather type information.
|
// Gather type information.
|
||||||
$instances = field_info_instances($this->entity_type, $this->bundle);
|
$instances = field_info_instances($this->entity_type, $this->bundle);
|
||||||
$field_types = field_info_field_types();
|
$field_types = field_info_field_types();
|
||||||
|
@ -407,9 +407,9 @@ class DisplayOverview extends OverviewBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overrides \Drupal\field_ui\OverviewBase::submit().
|
* Overrides \Drupal\field_ui\OverviewBase::submitForm().
|
||||||
*/
|
*/
|
||||||
public function submit(array &$form, array &$form_state) {
|
public function submitForm(array &$form, array &$form_state) {
|
||||||
$form_values = $form_state['values'];
|
$form_values = $form_state['values'];
|
||||||
$display = entity_get_display($this->entity_type, $this->bundle, $this->view_mode);
|
$display = entity_get_display($this->entity_type, $this->bundle, $this->view_mode);
|
||||||
|
|
||||||
|
|
|
@ -50,9 +50,9 @@ class FieldOverview extends OverviewBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::build().
|
* Implements \Drupal\Core\Form\FormInterface::buildForm().
|
||||||
*/
|
*/
|
||||||
public function build(array $form, array &$form_state) {
|
public function buildForm(array $form, array &$form_state) {
|
||||||
// When displaying the form, make sure the list of fields is up-to-date.
|
// When displaying the form, make sure the list of fields is up-to-date.
|
||||||
if (empty($form_state['post'])) {
|
if (empty($form_state['post'])) {
|
||||||
field_info_cache_clear();
|
field_info_cache_clear();
|
||||||
|
@ -420,9 +420,9 @@ class FieldOverview extends OverviewBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overrides \Drupal\field_ui\OverviewBase::validate().
|
* Implements \Drupal\Core\Form\FormInterface::validateForm().
|
||||||
*/
|
*/
|
||||||
public function validate(array &$form, array &$form_state) {
|
public function validateForm(array &$form, array &$form_state) {
|
||||||
$this->validateAddNew($form, $form_state);
|
$this->validateAddNew($form, $form_state);
|
||||||
$this->validateAddExisting($form, $form_state);
|
$this->validateAddExisting($form, $form_state);
|
||||||
}
|
}
|
||||||
|
@ -435,7 +435,7 @@ class FieldOverview extends OverviewBase {
|
||||||
* @param array $form_state
|
* @param array $form_state
|
||||||
* A reference to a keyed array containing the current state of the form.
|
* A reference to a keyed array containing the current state of the form.
|
||||||
*
|
*
|
||||||
* @see Drupal\field_ui\FieldOverview::validate()
|
* @see Drupal\field_ui\FieldOverview::validateForm()
|
||||||
*/
|
*/
|
||||||
protected function validateAddNew(array $form, array &$form_state) {
|
protected function validateAddNew(array $form, array &$form_state) {
|
||||||
$field = $form_state['values']['fields']['_add_new_field'];
|
$field = $form_state['values']['fields']['_add_new_field'];
|
||||||
|
@ -524,9 +524,9 @@ class FieldOverview extends OverviewBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overrides \Drupal\field_ui\OverviewBase::submit().
|
* Overrides \Drupal\field_ui\OverviewBase::submitForm().
|
||||||
*/
|
*/
|
||||||
public function submit(array &$form, array &$form_state) {
|
public function submitForm(array &$form, array &$form_state) {
|
||||||
$form_values = $form_state['values']['fields'];
|
$form_values = $form_state['values']['fields'];
|
||||||
|
|
||||||
$bundle_settings = field_bundle_settings($this->entity_type, $this->bundle);
|
$bundle_settings = field_bundle_settings($this->entity_type, $this->bundle);
|
||||||
|
|
|
@ -61,15 +61,15 @@ abstract class OverviewBase implements FormInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::validate().
|
* Implements \Drupal\Core\Form\FormInterface::validateForm().
|
||||||
*/
|
*/
|
||||||
public function validate(array &$form, array &$form_state) {
|
public function validateForm(array &$form, array &$form_state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::submit().
|
* Implements \Drupal\Core\Form\FormInterface::submitForm().
|
||||||
*/
|
*/
|
||||||
public function submit(array &$form, array &$form_state) {
|
public function submitForm(array &$form, array &$form_state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -34,12 +34,12 @@ class FormObjectTest extends WebTestBase {
|
||||||
*/
|
*/
|
||||||
function testObjectFormCallback() {
|
function testObjectFormCallback() {
|
||||||
$this->drupalGet('form-test/object-builder');
|
$this->drupalGet('form-test/object-builder');
|
||||||
$this->assertText('The FormTestObject::build() method was used for this form.');
|
$this->assertText('The FormTestObject::buildForm() method was used for this form.');
|
||||||
$elements = $this->xpath('//form[@id="form-test-form-test-object"]');
|
$elements = $this->xpath('//form[@id="form-test-form-test-object"]');
|
||||||
$this->assertTrue(!empty($elements), 'The correct form ID was used.');
|
$this->assertTrue(!empty($elements), 'The correct form ID was used.');
|
||||||
$this->drupalPost('form-test/object-builder', NULL, t('Save'));
|
$this->drupalPost('form-test/object-builder', NULL, t('Save'));
|
||||||
$this->assertText('The FormTestObject::validate() method was used for this form.');
|
$this->assertText('The FormTestObject::validateForm() method was used for this form.');
|
||||||
$this->assertText('The FormTestObject::submit() method was used for this form.');
|
$this->assertText('The FormTestObject::submitForm() method was used for this form.');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3283,7 +3283,7 @@ function system_config_form($form, &$form_state) {
|
||||||
if (!isset($form['#submit'])) {
|
if (!isset($form['#submit'])) {
|
||||||
$form['#submit'] = array();
|
$form['#submit'] = array();
|
||||||
if (isset($form_state['build_info']['callback_object'])) {
|
if (isset($form_state['build_info']['callback_object'])) {
|
||||||
$form['#submit'][] = array($form_state['build_info']['callback_object'], 'submit');
|
$form['#submit'][] = array($form_state['build_info']['callback_object'], 'submitForm');
|
||||||
}
|
}
|
||||||
elseif (function_exists($form_state['build_info']['form_id'] . '_submit')) {
|
elseif (function_exists($form_state['build_info']['form_id'] . '_submit')) {
|
||||||
$form['#submit'][] = $form_state['build_info']['form_id'] . '_submit';
|
$form['#submit'][] = $form_state['build_info']['form_id'] . '_submit';
|
||||||
|
|
|
@ -22,10 +22,10 @@ class FormTestObject implements FormInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::build().
|
* Implements \Drupal\Core\Form\FormInterface::buildForm().
|
||||||
*/
|
*/
|
||||||
public function build(array $form, array &$form_state) {
|
public function buildForm(array $form, array &$form_state) {
|
||||||
$form['element'] = array('#markup' => 'The FormTestObject::build() method was used for this form.');
|
$form['element'] = array('#markup' => 'The FormTestObject::buildForm() method was used for this form.');
|
||||||
|
|
||||||
$form['actions']['#type'] = 'actions';
|
$form['actions']['#type'] = 'actions';
|
||||||
$form['actions']['submit'] = array(
|
$form['actions']['submit'] = array(
|
||||||
|
@ -36,17 +36,17 @@ class FormTestObject implements FormInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::validate().
|
* Implements \Drupal\Core\Form\FormInterface::validateForm().
|
||||||
*/
|
*/
|
||||||
public function validate(array &$form, array &$form_state) {
|
public function validateForm(array &$form, array &$form_state) {
|
||||||
drupal_set_message(t('The FormTestObject::validate() method was used for this form.'));
|
drupal_set_message(t('The FormTestObject::validateForm() method was used for this form.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\Form\FormInterface::submit().
|
* Implements \Drupal\Core\Form\FormInterface::submitForm().
|
||||||
*/
|
*/
|
||||||
public function submit(array &$form, array &$form_state) {
|
public function submitForm(array &$form, array &$form_state) {
|
||||||
drupal_set_message(t('The FormTestObject::submit() method was used for this form.'));
|
drupal_set_message(t('The FormTestObject::submitForm() method was used for this form.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue