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

评论 (写第一个评论)