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

评论 (写第一个评论)