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

 
Bí quyết bán hành mỹ phẩm trên facebook cá nhân chất nhất 2019

Bí quyết bán hàng mỹ phẩm trên facebook cá nhân chất nhất 2019

Mua Follow; Từ khóa hành vi tìm kiếm ở phần mô tả; Từ khóa tìm kiếm ở tên phụ

CNTT Việt Nam 2011 - Nông dân được ‘lên mây’

CNTT Việt Nam 2011 - Nông dân được ‘lên mây’

Năm 2011 đã trôi qua khá trầm lắng trên thị trường CNTT Việt Nam. Bối cảnh kinh tế khó khăn đã để lại dấu ấn sâu sắc trong bức tranh toàn cảnh.

[May 15, 2014] Việc làm Drupal cho Web Developers

[May 15, 2014] Việc làm Drupal cho Web Developers

We are Midwest-based near the University of Notre Dame, two hours from Chicago, in beautiful Mishawaka, Indiana. Our cost of living is low. Our drives are short.

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

 

Diet con trung