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'];
|
||||
}
|
||||
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.
|
||||
|
@ -1078,7 +1078,7 @@ function drupal_prepare_form($form_id, &$form, &$form_state) {
|
|||
// Ensure that modules can rely on #validate being set.
|
||||
$form['#validate'] = array();
|
||||
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.
|
||||
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.
|
||||
$form['#submit'] = array();
|
||||
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.
|
||||
elseif (function_exists($form_id . '_submit')) {
|
||||
|
|
|
@ -31,7 +31,7 @@ interface FormInterface {
|
|||
* @return array
|
||||
* The form structure.
|
||||
*/
|
||||
public function build(array $form, array &$form_state);
|
||||
public function buildForm(array $form, array &$form_state);
|
||||
|
||||
/**
|
||||
* Form validation handler.
|
||||
|
@ -41,7 +41,7 @@ interface FormInterface {
|
|||
* @param array $form_state
|
||||
* 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.
|
||||
|
@ -51,6 +51,6 @@ interface FormInterface {
|
|||
* @param array $form_state
|
||||
* 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.
|
||||
*/
|
||||
public function build(array $form, array &$form_state) {
|
||||
public function buildForm(array $form, array &$form_state) {
|
||||
$entities = $this->load();
|
||||
$form['#attached']['css'][] = drupal_get_path('module', 'block') . '/block.admin.css';
|
||||
$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.
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Core\Form\FormInterface::submit().
|
||||
* Implements \Drupal\Core\Form\FormInterface::submitForm().
|
||||
*
|
||||
* 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']));
|
||||
foreach ($entities as $entity_id => $entity) {
|
||||
$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.
|
||||
$instances = field_info_instances($this->entity_type, $this->bundle);
|
||||
$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'];
|
||||
$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.
|
||||
if (empty($form_state['post'])) {
|
||||
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->validateAddExisting($form, $form_state);
|
||||
}
|
||||
|
@ -435,7 +435,7 @@ class FieldOverview extends OverviewBase {
|
|||
* @param array $form_state
|
||||
* 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) {
|
||||
$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'];
|
||||
|
||||
$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() {
|
||||
$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"]');
|
||||
$this->assertTrue(!empty($elements), 'The correct form ID was used.');
|
||||
$this->drupalPost('form-test/object-builder', NULL, t('Save'));
|
||||
$this->assertText('The FormTestObject::validate() method was used for this form.');
|
||||
$this->assertText('The FormTestObject::submit() method was used for this form.');
|
||||
$this->assertText('The FormTestObject::validateForm() 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'])) {
|
||||
$form['#submit'] = array();
|
||||
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')) {
|
||||
$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) {
|
||||
$form['element'] = array('#markup' => 'The FormTestObject::build() method was used for this form.');
|
||||
public function buildForm(array $form, array &$form_state) {
|
||||
$form['element'] = array('#markup' => 'The FormTestObject::buildForm() method was used for this form.');
|
||||
|
||||
$form['actions']['#type'] = 'actions';
|
||||
$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) {
|
||||
drupal_set_message(t('The FormTestObject::validate() method was used for this form.'));
|
||||
public function validateForm(array &$form, array &$form_state) {
|
||||
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) {
|
||||
drupal_set_message(t('The FormTestObject::submit() method was used for this form.'));
|
||||
public function submitForm(array &$form, array &$form_state) {
|
||||
drupal_set_message(t('The FormTestObject::submitForm() method was used for this form.'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue