11.7 实体字段

Drupal8龩龧龦龣龟龜龚龗龔龐龏龍

  • boolean 齿齽齹
  • changed 齲齰齭齫
  • created 齦齢齭齫
  • decimal 齙龟
  • email 齖齕齔齑
  • entity_reference 龦龣齉齅
  • float 齄齃齙
  • integer 齁齹
  • language 齀鼼
  • map 鼻鼷
  • password 鼱鼰
  • string_long 鼬鼪鼧
  • timestamp 鼦鼢鼟
  • uri 鼛鼙鼗鼕鼓鼏鼋
  • uuid 鼈鼙id
  • comment 鼄鼀
  • datetime 鼦鼢齭齫
  • file 鼪齑
  • image 黽黹
  • link 黳黰
  • list_float 齄齃齙黮黫
  • list_integer 齁齙黮黫
  • list_string 龟鼋黧黮黫
  • path 黤黢
  • telephone 齖黟
  • text 鼪鼧
  • text_long 鼬鼪鼧
  • text_with_summary 黚黙龗龧鼪鼧

1黖黔鼓黓龟龜黑齹(Field Type)

modules/custom/hello_world/src/Plugin/Field/FieldType/EntityUserAccessField.php

龟龜黑齹麵麲麯@FieldType麫麧

  • id: 龟龜黑齹龧麠麟麞麝鼈鼙ID麤
  • label: 齅麖齅麔麑麎龧麊麉麤
  • description: 龟龜麆麄
namespace Drupal\MODULENAME\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
 * @FieldType(
 *   id = "entity_user_access",
 *   label = @Translation("Entity User Access"),
 *   description = @Translation("This field stores a reference to a user and a password for this user on the entity."),
 * )
 */

class EntityUserAccessField extends FieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['uid'] = DataDefinition::create('integer')
      ->setLabel(t('User ID Reference'))
      ->setDescription(t('The ID of the referenced user.'))
      ->setSetting('unsigned', TRUE);

    $properties['password'] = DataDefinition::create('string')
      ->setLabel(t('Password'))
      ->setDescription(t('A password saved in plain text. That is not save dude!'));

    // ToDo: Add more Properties.

    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $columns = array(
      'uid' => array(
        'description' => 'The ID of the referenced user.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
      'password' => array(
        'description' => 'A plain text password.',
        'type' => 'varchar',
      ),
      'created' => array(
        'description' => 'A timestamp of when this entry has been created.',
        'type' => 'timestamp',
      ),

      // ToDo: Add more columns.
    );

    $schema = array(
      'columns' => $columns,
      'indexes' => array(),
      'foreign keys' => array(),
    );

    return $schema;
  }
}

龦鹆鹄2鸾鸼鸸龍

  • propertyDefinitions: 鸴baseFieldDefinition黑鸳麝鼓黓龟龜龧鸯鸮麤
  • schema: 鸬鸪Drupal鸦鸤齦齢龟龜麤

2黖黔鼓黓龟龜鸡齑(Field Widget)

龟龜鸡齑齅麖鸜鹆齅麔鸘鸗黫鸖龧齙鸒麤鸦:

  • 鸐龗黫鸖鸘鸗鼙鸾齁齙麝鸌齅麔鸈麯鸇鸅鸁鷾checkbox麯龦鹆鸘鸗麤
  • 鷽鷻齅黔鷹鷵鷲鷱鷭鸘鸗齙鸒麤
  • 鼱鼰鸘鸗龔鷫鷧龧UI麤

modules/custom/hello_world/src/Plugin/Field/FieldWidget/EntityUserAccessWidget.php

龟龜鸡齑麵麲麯@FieldWidget麫麧

  • id: 鸡齑龧麠麟麞
  • field_types: 龐鸾龟龜鷭鷣齅龧龟龜黑齹齙鷡
  • multiple_values: 鷞鷝鷜FALSE麤鸦鷘鷗鷓TRUE鷏鸈麯鷍鷽鷋龦龣黫鸖鷇鷃鷂鶿龧鶼麤
namespace Drupal\MODULENAME\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'entity_user_access_w' widget.
 *
 * @FieldWidget(
 *   id = "entity_user_access_w",
 *   label = @Translation("Entity User Access - Widget"),
 *   description = @Translation("Entity User Access - Widget"),
 *   field_types = {
 *     "entity_user_access",
 *   },
 *   multiple_values = TRUE,
 * )
 */

class EntityUserAccessWidget extends WidgetBase {
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['userlist'] = array(
      '#type' => 'select',
      '#title' => t('User'),
      '#description' => t('Select group members from the list.'),
      '#options' => array(
         0 => t('Anonymous'),
         1 => t('Admin'),
         2 => t('foobar'),
         // This should be implemented in a better way!
       ),
    );

    $element['passwordlist'] = array(
      '#type' => 'password',
      '#title' => t('Password'),
      '#description' => t('Select a password for the user'),
    );

    return $element;
  }
}

鷽鹆鶰鶯麫黚龔龐鸾鸡齑龧黫鸖麝鷽鶮鶫鶪鶦鶢龔鶠鸾鸘鸗龟龜麝鼙鸾鷓齅麖鸘鸗齅麔麞麝鶟鼙鸾齅麖鸘鸗鼱鼰麤鸦鷘鷽鷻鶜鶛黫鸖齙鸒麝鷽鸐龗龦鹆龐鸾鸡齑龧鶜鶛鸼鸸麤

3黖黔鼓黓龟龜鶗鶔(Field Formatter)

龟龜鶗鶔齅麖鸜鹆齙鸒麝龐鶒鶎龦龣龧鶋鶫鶇鶔麤鼙鶆鶂麝鸡齑鷓齅麖黫鸖鶇鶔麝龟龜鶗鶔齅麖龦龣龟龜龧鶋鶫鶇鶔麤

namespace Drupal\MODULENAME\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * Plugin implementation of the 'entity_user_access_f' formatter.
 *
 * @FieldFormatter(
 *   id = "entity_user_access_f",
 *   label = @Translation("Entity User Access - Formatter"),
 *   description = @Translation("Entity User Access - Formatter"),
 *   field_types = {
 *     "entity_user_access",
 *   }
 * )
 */

class EntityUserAccessFormatter extends FormatterBase {
  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = array();

    foreach ($items as $delta => $item) {
      $elements[$delta] = array(
        'uid' => array(
          '#markup' => \Drupal\user\Entity\User::load($item->user_id)->getUsername(),
        ),
        // Add more content
      );
    }

    return $elements;
  }
}
  • viewElements()鶀鵾鵽龧龟龜龧齅麔麞鵼鸗$elements齙鷡龩麤鶰龦龣龩鵹鵷龦鹆鵶鵲鸡鵮麝龐鸾龦鹆鶀鶮鵬鵨龔鵥鵶鵲龦龣龧齅麔麞麤

本书共79小节:

评论 (写第一个评论)