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

评论 (写第一个评论)