[Phần 2] Hướng dẫn creating a custom field : Field widget trong Drupal 8

[Phần 2] Hướng dẫn creating a custom field : Field widget trong Drupal 8

This is part 2 in my series of articles about creating a custom field. I recommend reading Part 1: Field type first, if you have not done so already.

After creating the field type it is now time to create the field widget.

a) Create the file

The field widget must be located as follows:

<module_name>/lib/Drupal/<module_name>/Plugin/field/widget/<field_widget_name>.php
N.B. The field widget name should be in CamelCase.

b) Add Contains, namespace and use

In the newly created field type file add a brief comment to explain what it consists of:


/**
 * @file
 * Contains \Drupal\<module_name>\Plugin\field\widget\<field_widget_name>.
 */

N.B. The "Contains..." line should match the location and name of this file.

Then add the namespace as follows:


namespace Drupal\<module_name>\Plugin\field\widget;

N.B. I cannot emphasise enough: it is vital that the namespace matches the location of the file otherwise it will not work.

Then add the following uses:


use Drupal\Core\Entity\Field\FieldItemListInterface;

This provides a variable type required within the field widget class.


use Drupal\field\Plugin\Type\Widget\WidgetBase;

This provides the class that the field widget will extend.

c) Add widget details annotation

The annotation should appear as follows:


/**
 * Plugin implementation of the '<field_widget_id>' widget.
 *
 * @FieldWidget(
 *   id = "<field_widget_id>",
 *   label = @Translation("<field_widget_label>"),
 *   field_types = {
 *     "<field_type_id>"
 *   }
 * )
 */

N.B. All text represented by a <placeholder> should be appropriately replaced according to requirements. The field_type_id must match the id of a field type and the field_widget_id should match the default widget specified in the field type (see Part 1 of this article).

d) Add field widget class

Create the field widget class as follows:


class <field_widget_name> extends WidgetBase {

}

N.B. The <field_widget_name> must match the name of this file (case-sensitive).

The field widget class needs to contain the formElement() function that defines how the field will appear on data input forms:


 /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {

    $element['forename'] = array(
      '#title' => t('Forename'),
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]->forename) ? $items[$delta]->forename : NULL,
    );
    $element['surname'] = array(
      '#title' => t('Surname'),
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]->surname) ? $items[$delta]->surname : NULL,
    );
    $element['age'] = array(
      '#title' => t('Age'),
      '#type' => 'number',
      '#default_value' => isset($items[$delta]->age) ? $items[$delta]->age : NULL,
    );
    return $element;
  }

The above example includes element types of textfield and number, other element types include:

  • radios
  • checkboxes
  • email
  • url

I intend to delve into other element types in a future article.

And there we have it: a complete (basic) field widget. Here is a simple example, similar to that described above.

Bạn thấy bài viết này như thế nào?: 
Average: 5 (1 vote)
Ả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.

Tìm kiếm bất động sản

 

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

 
Search engine optimization (SEO)

Thêm Meta Tags vào 1 node của Drupal Content

Search engine optimization (SEO) is often at the top of the to-do list when planning a site.

10 ứng dụng thương mại điện tử mã nguồn mở miễn phí

10 ứng dụng thương mại điện tử mã nguồn mở miễn phí

Thương mại điện tử ở Việt Nam đang ngày càng phát triển và kinh doanh qua mạng đang trở nên hot hơn bất cứ lúc nào. Vì vậy, việc xây dựng cho riêng mình 1 website bán hàng trực tuyến là nhu cầu của rất nhiều người. Dưới đây là một số phần mềm miễn phí (Opensource PHP E-Commerce) và được nhiều người sử dụng để các bạn có thể tham khảo và lựa chọn.

Yandex

Công cụ tìm kiếm Yandex số 1 ở Nga

Sau giờ làm việc buổi chiều, tôi  ngồi theo dõi mấy Crawler và Spider đang Index site, thấy có Yandex.ru

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

 

Diet con trung