Commit 6a6e601d authored by webchick's avatar webchick
Browse files

Issue #2309323 by Berdir, tim.plunkett: Allow #submit and #validate to be...

Issue #2309323 by Berdir, tim.plunkett: Allow #submit and #validate to be specified as methods of the form object.
parent 37586d97
......@@ -138,7 +138,7 @@ public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
// Add a process callback.
$form['#process'][] = array($this, 'processForm');
$form['#process'][] = '::processForm';
if (!isset($form['langcode'])) {
// If the form did not specify otherwise, default to keeping the existing
......@@ -210,13 +210,8 @@ protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save'),
'#validate' => array(
array($this, 'validate'),
),
'#submit' => array(
array($this, 'submit'),
array($this, 'save'),
),
'#validate' => array('::validate'),
'#submit' => array('::submit', '::save'),
);
if (!$this->entity->isNew() && $this->entity->hasLinkTemplate('delete-form')) {
......@@ -317,11 +312,6 @@ protected function updateFormLangcode(FormStateInterface $form_state) {
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = clone $this->entity;
// If you submit a form, the form state comes from caching, which forces
// the form object to be the one before caching. Ensure to have the
// form object of the current request.
$form_state['controller'] = $this;
$this->copyFormValuesToEntity($entity, $form, $form_state);
// Invoke all specified builders for copying form values to entity
......
......@@ -639,8 +639,8 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state) {
$form += $this->getElementInfo('form');
$form += array('#tree' => FALSE, '#parents' => array());
$form['#validate'][] = array($form_state['build_info']['callback_object'], 'validateForm');
$form['#submit'][] = array($form_state['build_info']['callback_object'], 'submitForm');
$form['#validate'][] = '::validateForm';
$form['#submit'][] = '::submitForm';
// If no #theme has been set, automatically apply theme suggestions.
// theme_form() itself is in #theme_wrappers and not #theme. Therefore, the
......@@ -767,9 +767,9 @@ public function doBuildForm($form_id, &$element, FormStateInterface &$form_state
// Allow for elements to expand to multiple elements, e.g., radios,
// checkboxes and files.
if (isset($element['#process']) && !$element['#processed']) {
foreach ($element['#process'] as $process) {
foreach ($element['#process'] as $callback) {
$complete_form = &$form_state->getCompleteForm();
$element = call_user_func_array($process, array(&$element, &$form_state, &$complete_form));
$element = call_user_func_array($form_state->prepareCallback($callback), array(&$element, &$form_state, &$complete_form));
}
$element['#processed'] = TRUE;
}
......
......@@ -796,6 +796,17 @@ public function setRebuild($rebuild = TRUE) {
return $this;
}
/**
* {@inheritdoc}
*/
public function prepareCallback($callback) {
if (is_string($callback) && substr($callback, 0, 2) == '::') {
$callback = array($this->get('build_info')['callback_object'], substr($callback, 2));
}
return $callback;
}
/**
* Wraps drupal_set_message().
*
......
......@@ -456,4 +456,18 @@ public function getError($element);
*/
public function setRebuild($rebuild = TRUE);
/**
* Converts support notations for a form callback to a valid callable.
*
* Specifically, supports methods on the form/callback object as strings when
* they start with ::, for example "::submitForm()".
*
* @param string|array $callback
* The callback.
*
* @return array|string
* A valid callable.
*/
public function prepareCallback($callback);
}
......@@ -105,7 +105,7 @@ public function executeSubmitHandlers(&$form, FormStateInterface &$form_state) {
$handlers = array();
}
foreach ($handlers as $function) {
foreach ($handlers as $callback) {
// Check if a previous _submit handler has set a batch, but make sure we
// do not react to a batch that is already being processed (for instance
// if a batch operation performs a
......@@ -114,11 +114,11 @@ public function executeSubmitHandlers(&$form, FormStateInterface &$form_state) {
// Some previous submit handler has set a batch. To ensure correct
// execution order, store the call in a special 'control' batch set.
// See _batch_next_set().
$batch['sets'][] = array('form_submit' => $function);
$batch['sets'][] = array('form_submit' => $callback);
$batch['has_form_submits'] = TRUE;
}
else {
call_user_func_array($function, array(&$form, &$form_state));
call_user_func_array($form_state->prepareCallback($callback), array(&$form, &$form_state));
}
}
}
......
......@@ -79,8 +79,8 @@ public function executeValidateHandlers(&$form, FormStateInterface &$form_state)
$handlers = array();
}
foreach ($handlers as $function) {
call_user_func_array($function, array(&$form, &$form_state));
foreach ($handlers as $callback) {
call_user_func_array($form_state->prepareCallback($callback), array(&$form, &$form_state));
}
}
......@@ -263,7 +263,7 @@ protected function doValidateForm(&$elements, FormStateInterface &$form_state, $
elseif (isset($elements['#element_validate'])) {
foreach ($elements['#element_validate'] as $callback) {
$complete_form = &$form_state->getCompleteForm();
call_user_func_array($callback, array(&$elements, &$form_state, &$complete_form));
call_user_func_array($form_state->prepareCallback($callback), array(&$elements, &$form_state, &$complete_form));
}
}
......
......@@ -104,7 +104,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
array('driver'),
array($default_driver),
),
'#submit' => array(array($this, 'submitForm')),
'#submit' => array('::submitForm'),
);
$form['errors'] = array();
......
......@@ -101,7 +101,7 @@ public function form(array $form, FormStateInterface $form_state) {
'#title' => t('Theme'),
'#default_value' => $theme,
'#ajax' => array(
'callback' => array($this, 'themeSwitch'),
'callback' => '::themeSwitch',
'wrapper' => 'edit-block-region-wrapper',
),
);
......
......@@ -244,13 +244,8 @@ protected function actions(array $form, FormStateInterface $form_state) {
'#type' => 'submit',
'#value' => $this->t('Preview'),
'#access' => $preview_mode != DRUPAL_DISABLED,
'#validate' => array(
array($this, 'validate'),
),
'#submit' => array(
array($this, 'submit'),
array($this, 'preview'),
),
'#validate' => array('::validate'),
'#submit' => array('::submit', '::preview'),
);
return $element;
......
......@@ -95,7 +95,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $config_t
'#options' => $config_types,
'#default_value' => $config_type,
'#ajax' => array(
'callback' => array($this, 'updateConfigurationType'),
'callback' => '::updateConfigurationType',
'wrapper' => 'edit-config-type-wrapper',
),
);
......@@ -109,7 +109,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $config_t
'#prefix' => '<div id="edit-config-type-wrapper">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => array($this, 'updateExport'),
'callback' => '::updateExport',
'wrapper' => 'edit-export-wrapper',
),
);
......
......@@ -156,13 +156,8 @@ public function actions(array $form, FormStateInterface $form_state) {
$elements['submit']['#value'] = $this->t('Send message');
$elements['preview'] = array(
'#value' => $this->t('Preview'),
'#validate' => array(
array($this, 'validate'),
),
'#submit' => array(
array($this, 'submit'),
array($this, 'preview'),
),
'#validate' => array('::validate'),
'#submit' => array('::submit', '::preview'),
);
return $elements;
}
......
......@@ -59,7 +59,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'submit',
'#value' => $this->t('Reset'),
'#limit_validation_errors' => array(),
'#submit' => array(array($this, 'resetForm')),
'#submit' => array('::resetForm'),
);
}
return $form;
......
......@@ -189,7 +189,7 @@ public function buildForm(array $form, FormStateInterface $form_state, FilterFor
// No regular submit-handler. This form only works via JavaScript.
'#submit' => array(),
'#ajax' => array(
'callback' => array($this, 'submitForm'),
'callback' => '::submitForm',
'event' => 'click',
),
);
......
......@@ -69,7 +69,7 @@ public function buildForm(array $form, FormStateInterface $form_state, FilterFor
// No regular submit-handler. This form only works via JavaScript.
'#submit' => array(),
'#ajax' => array(
'callback' => array($this, 'submitForm'),
'callback' => '::submitForm',
'event' => 'click',
),
);
......
......@@ -157,7 +157,7 @@ public function buildForm(array $form, FormStateInterface $form_state, FieldInst
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => $this->t('Delete field'),
'#submit' => array(array($this, 'delete')),
'#submit' => array('::delete'),
);
return $form;
}
......
......@@ -99,7 +99,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// The form needs to have submit and validate handlers set explicitly.
// Use the existing taxonomy overview submit handler.
$form['#submit'] = array(array($this, 'submitForm'));
$form['#submit'] = array('::submitForm');
$form['terms']['#empty'] = $this->t('No containers or forums available. <a href="@container">Add container</a> or <a href="@forum">Add forum</a>.', array(
'@container' => $this->url('forum.add_container'),
'@forum' => $this->url('forum.add_forum')
......
......@@ -49,8 +49,8 @@ public function form(array $form, FormStateInterface $form_state) {
'select#edit-predefined-langcode' => array('value' => 'custom'),
),
),
'#validate' => array(array($this, 'validatePredefined')),
'#submit' => array(array($this, 'submitForm')),
'#validate' => array('::validatePredefined'),
'#submit' => array('::submitForm'),
);
$custom_language_states_conditions = array(
......@@ -72,8 +72,8 @@ public function form(array $form, FormStateInterface $form_state) {
$form['custom_language']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Add custom language'),
'#validate' => array(array($this, 'validateCustom')),
'#submit' => array(array($this, 'submitForm')),
'#validate' => array('::validateCustom'),
'#submit' => array('::submitForm'),
);
return $form;
......
......@@ -38,8 +38,8 @@ public function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save language'),
'#validate' => array(array($this, 'validateCommon')),
'#submit' => array(array($this, 'submitForm')),
'#validate' => array('::validateCommon'),
'#submit' => array('::submitForm'),
);
return $actions;
}
......
......@@ -74,7 +74,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['filters']['actions']['reset'] = array(
'#type' => 'submit',
'#value' => $this->t('Reset'),
'#submit' => array(array($this, 'resetForm')),
'#submit' => array('::resetForm'),
);
}
......
......@@ -288,7 +288,7 @@ protected function actions(array $form, FormStateInterface $form_state) {
$element['publish']['#value'] = $node->isPublished() ? t('Save and keep published') : t('Save and publish');
}
$element['publish']['#weight'] = 0;
array_unshift($element['publish']['#submit'], array($this, 'publish'));
array_unshift($element['publish']['#submit'], '::publish');
// Add a "Unpublish" button.
$element['unpublish'] = $element['submit'];
......@@ -300,7 +300,7 @@ protected function actions(array $form, FormStateInterface $form_state) {
$element['unpublish']['#value'] = !$node->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
}
$element['unpublish']['#weight'] = 10;
array_unshift($element['unpublish']['#submit'], array($this, 'unpublish'));
array_unshift($element['unpublish']['#submit'], '::unpublish');
// If already published, the 'publish' button is primary.
if ($node->isPublished()) {
......@@ -321,13 +321,8 @@ protected function actions(array $form, FormStateInterface $form_state) {
'#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
'#value' => t('Preview'),
'#weight' => 20,
'#validate' => array(
array($this, 'validate'),
),
'#submit' => array(
array($this, 'submit'),
array($this, 'preview'),
),
'#validate' => array('::validate'),
'#submit' => array('::submit', '::preview'),
);
$element['delete']['#access'] = $node->access('delete');
......
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