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

 
Hot girl bánh tráng trộn chào xuân trong bộ ảnh mới

Hot girl bánh tráng trộn chào xuân trong bộ ảnh mới

Những hình ảnh mới nhất của hot girl bánh tráng trộn trước thềm năm mới khiến người xem thích thú.

Scala

Web framework và web server Xitrum cho Scala do người Việt viết

Xitrum vừa là web framework vừa là web server mã nguồn mở theo phong cách hiện đại viết bằng Scala, cho Scala, có những tính năng nổi bật sau:

Ronaldinho, Ronaldo, Adriano, Robinho và Pato tất cả đều nghiện sex

Ronaldinho, Ronaldo de Lima, Adriano, Robinho và Pato là những tài năng sáng giá nhất ở Brazil trong thời của họ. Tuy nhiên, những "vũ công Samba" này lại không thể giữ đôi chân trên mặt đất khi họ đang ở đỉnh cao của sự nghiệp.

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

 

Diet con trung