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小节:

评论 (写第一个评论)