Commit e898c4dd authored by webchick's avatar webchick

Issue #1925140 by tim.plunkett: Fixed drupal_form_submit() does not accept FormInterface objects.

parent 74804f70
......@@ -101,6 +101,31 @@
* See drupal_build_form() for documentation of $form_state keys.
*/
/**
* Determines the form ID.
*
* @param \Drupal\Core\Form\FormInterface|string $form_arg
* A form object to use to build the form, or the unique string identifying
* the desired form. If $form_arg is a string and a function with that
* name exists, it is called to build the form array.
* @param array $form_state
* An associative array containing the current state of the form.
*
* @return string
* The unique string identifying the desired form.
*/
function _drupal_form_id($form_arg, &$form_state) {
// If the $form_arg implements \Drupal\Core\Form\FormInterface, add that as
// the callback object and determine the form ID.
if (is_object($form_arg) && $form_arg instanceof FormInterface) {
$form_state['build_info']['callback_object'] = $form_arg;
return $form_arg->getFormID();
}
// Otherwise, the $form_arg is the form ID.
return $form_arg;
}
/**
* Returns a renderable form array for a given form ID.
*
......@@ -137,15 +162,7 @@ function drupal_get_form($form_arg) {
array_shift($args);
$form_state['build_info']['args'] = $args;
// Determine the form ID.
if (is_object($form_arg) && $form_arg instanceof FormInterface) {
$form_state['build_info']['callback_object'] = $form_arg;
$form_id = $form_arg->getFormID();
}
else {
$form_id = $form_arg;
}
$form_id = _drupal_form_id($form_arg, $form_state);
return drupal_build_form($form_id, $form_state);
}
......@@ -648,13 +665,14 @@ function form_load_include(&$form_state, $type, $module, $name = NULL) {
* There is no return value, but you can check to see if there are errors
* by calling form_get_errors().
*
* @param $form_id
* The unique string identifying the desired form. If a function
* with that name exists, it is called to build the form array.
* Modules that need to generate the same form (or very similar forms)
* using different $form_ids can implement hook_forms(), which maps
* different $form_id values to the proper form constructor function. Examples
* may be found in node_forms() and search_forms().
* @param \Drupal\Core\Form\FormInterface|string $form_arg
* A form object to use to build the form, or the unique string identifying
* the desired form. If $form_arg is a string and a function with that
* name exists, it is called to build the form array. Modules that need to
* generate the same form (or very similar forms) using different $form_ids
* can implement hook_forms(), which maps different $form_id values to the
* proper form constructor function. Examples may be found in node_forms()
* and search_forms().
* @param $form_state
* A keyed array containing the current state of the form. Most important is
* the $form_state['values'] collection, a tree of data used to simulate the
......@@ -693,7 +711,7 @@ function form_load_include(&$form_state, $type, $module, $name = NULL) {
* drupal_form_submit('user_register_form', $form_state);
* @endcode
*/
function drupal_form_submit($form_id, &$form_state) {
function drupal_form_submit($form_arg, &$form_state) {
if (!isset($form_state['build_info']['args'])) {
$args = func_get_args();
array_shift($args);
......@@ -710,6 +728,8 @@ function drupal_form_submit($form_id, &$form_state) {
$form_state['input'] = $form_state['values'];
$form_state['programmed'] = TRUE;
$form_id = _drupal_form_id($form_arg, $form_state);
$form = drupal_retrieve_form($form_id, $form_state);
// Programmed forms are always submitted.
$form_state['submitted'] = TRUE;
......
......@@ -7,12 +7,13 @@
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
use Drupal\system\Tests\System\SystemConfigFormBase;
use Drupal\form_test\FormTestObject;
/**
* Tests building a form from an object.
*/
class FormObjectTest extends WebTestBase {
class FormObjectTest extends SystemConfigFormBase {
/**
* Modules to enable.
......@@ -29,6 +30,19 @@ public static function getInfo() {
);
}
protected function setUp() {
parent::setUp();
$this->form_id = new FormTestObject();
$this->values = array(
'bananas' => array(
'#value' => $this->randomString(10),
'#config_name' => 'form_test.object',
'#config_key' => 'bananas',
),
);
}
/**
* Tests using an object as the form callback.
*/
......
......@@ -27,6 +27,11 @@ public function getFormID() {
public function buildForm(array $form, array &$form_state) {
$form['element'] = array('#markup' => 'The FormTestObject::buildForm() method was used for this form.');
$form['bananas'] = array(
'#type' => 'textfield',
'#title' => t('Bananas'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
......@@ -47,6 +52,9 @@ public function validateForm(array &$form, array &$form_state) {
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestObject::submitForm() method was used for this form.'));
config('form_test.object')
->set('bananas', $form_state['values']['bananas'])
->save();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment