Hướng dẫn thay đổi Delete buttons thành links trong Drupal 8

Hướng dẫn thay đổi Delete buttons thành links trong Drupal 8

In Drupal 8 delete buttons switched to links instead of submit buttons.

Main reasons for this switch:

  1. In most cases those submit buttons were just triggering a redirection to a confirm form.
  2. 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

<?php
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

<?php
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);
  }
}
?>
Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Google TV 2.0 chạy vi xử lí ARM ra mắt tại CES 2012

Google TV 2.0 chạy vi xử lí ARM ra mắt tại CES 2012

Thế hệ Google TV tiếp theo sẽ sử dụng bộ vi xử lí ARM sau khi nhà sản xuất chip Marvell cho biết...

Lỗi hao pin iPhone 4S được Apple khắc phục

Lỗi hao pin iPhone 4S được Apple khắc phục

Apple đã chính thức đưa ra phiên bản iOS 5.0.1 để sửa lỗi hao pin mà người dùng gần đây phàn nàn trên các diễn đàn mạng.

Facebook muốn phủ sóng toàn thế giới

Facebook muốn phủ sóng toàn thế giới

Mạng xã hội hùng mạnh nhất thế giới hiện có khoảng 800 triệu thành viên, nhưng mục tiêu của Facebook không dừng lại ở đó.

Công ty diệt chuột T&C

 

Diet con trung