Custom Content Entity Field types of drupal 8

Submitted by editor on Sat, 11/12/2016 - 11:00
Question

What are the Field types of content Entity on drupal 8 ?

Entity Field types, Data types

(BaseFieldDefinition::create types)

  • tring: A simple string.
  • boolean: A boolean stored as an integer.
  • integer: An integer, with settings for min and max value validation (also provided for decimal and float)
  • decimal: A decimal with configurable precision and scale.
  • float: A float number
  • language: Contains a language code and the language as a computed property
  • timestamp: A Unix timestamp stored as an integer
  • created: A timestamp that uses the current time as default value.
  • changed: A timestamp that is automatically updated to the current time if the entity is saved.
  • date: A date stored as an ISO 8601 string.
  • datetime: A date stored as an ISO 8601 string.
  • uri: Contains a URI. The link module also provides a link field type that can include a link title and can point to an internal or external URI/route.
  • uuid: A UUID field that generates a new UUID as the default value.
  • email: An email, with corresponding validation and widgets and formatters.
  • entity_reference: An entity reference with a target_id and a computed entity field property. entity_reference.module provides widgets and formatters when enabled.
  • map: Can contain any number of arbitrary properties, stored as a serialized string
  • text_with_summary : Text area field with summery

https://www.drupal.org/docs/8/api/entity-api/defining-and-using-content-entity-field-definitions

Other Field types

  • file: A file, Photo ....
  • image:
  •  

 

Entity Field settings:

Example :

->setSetting('target_type', 'file')
->setSettings(array(
    'default_value' => '',
    'text_processing' => 0,
))

Type : File (file)

  'file_extensions' => 'txt',
  'file_directory' => '[date:custom:Y]-[date:custom:m]',
  'max_filesize' => '',
  'description_field' => 0,

Type : Image (image)

  'file_extensions' => 'png gif jpg jpeg',
  'alt_field' => 1,
  'alt_field_required' => 1,
  'title_field' => 0,
  'title_field_required' => 0,
  'max_resolution' => '',
  'min_resolution' => '',
  'default_image' => array(
    'uuid' => NULL,
    'alt' => '',
    'title' => '',
    'width' => NULL,
    'height' => NULL,
  ),

Type Select list (list_string)

    'default_value' => 'NEW',
    'max_length' => 16,
    'allowed_values' => array(
      'NEW' => 'New',
      'OK' => 'Ok',
      'ERR' => 'Error',
    ),

Example of Entity field : Text area with summary

$fields['description'] = BaseFieldDefinition::create('text_with_summary')
  ->setLabel(t('Description'))
  ->setSettings(array(
    'default_value' => '',
    'text_processing' => 0,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'text_textarea_with_summary',
    'settings' => array(
      'rows' => 4,
    ),
  ))
  ->setDisplayOptions('view', array(
    'type' => 'text_summary_or_trimmed',
    'settings' => array(
      'trim_length' => 600,
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

 

Display options

Example

  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'string',
    'weight' => -6,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'options_select',
    'weight' => -6,
  ))

Types :

  • string
  • options_select
  • boolean_checkbox
  • string_textfield
  • string_textarea

See also : How to create a custom plugin

 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.