Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ParisLiakos on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
In Drupal 8 delete buttons switched to links instead of submit buttons.
Main reasons for this switch:
- In most cases those submit buttons were just triggering a redirection to a confirm form.
- Because of the fact that they were submit buttons the form validation (server-side and client-side) was being triggered, which didn't make sense.
D7
function myform($form, &$form_state) {
$form = array();
// ...
// The rest of form declaration.
// ...
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array('myform_delete_submit'),
);
return $form;
}
function myform_delete_submit($form, &$form_state) {
$form_state['redirect'] = 'my/path/delete';
}
D8
use Drupal\Core\Form\FormBase;
class MyForm extends FormBase {
public function buildForm(array $form, array &$form_state) {
$form = array();
// ...
// The rest of form declaration.
// ...
$form['actions']['delete'] = array(
'#type' => 'link',
'#title' => $this->t('Delete'),
'#attributes' => array(
'class' => array('button', 'button--danger'),
),
'#route_name' => 'my.delete_route',
);
return parent::buildForm($form, $form_state);
}
}
Impacts:
Site builders, administrators, editors
Module developers