Custom Content Entity Field types of drupal 8 | Drupal 8

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)

  • string: A simple string.
  • string_long: A log string up To ~4GB
  • 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 : Simple string field with maximum 128 characters.

$fields["word"] = BaseFieldDefinition::create('string')
->setLabel("Word")
->setSettings([
  'default_value' => '',
  'text_processing' => 0,
  'max_length' => 128,
]);

Example : File

->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);

 

Specify a bundle (target_bundles) in entity reference field

Using this you can specify a node type, taxonomy_term's vocabulary in entity reference field.
Example :

$fields['tags'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Tags'))
  ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler_settings',
    [
      'target_bundles' => [
        'tags' => 'tags'
      ]
    ]
  )
  ->setDisplayOptions('view', array(
    'label' => 'above',
  ))
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'placeholder' => '',
    ),
  ));

 

Example of CKEditor WYSISYG field format

$fields['description'] = BaseFieldDefinition::create('text_long')
  ->setLabel(t('Description'))
  ->setSettings(array(
    'default_value' => '',
    'text_processing' => 0,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'text_textarea',
    'settings' => array(
      'rows' => 4,
    ),
  ))
  ->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,
  ))

For Views : $data['THE_TABLE']['THE_FIELD']['filter']['id'] = 'list_field';

Types :

  • string
  • options_select
  • boolean_checkbox
  • string_textfield
  • string_textarea
  • options_buttons (for boolean)
  • boolean_checkbox (default value of boolean)

See also : How to create a custom plugin

To use a date field of an entity on  Views filter, You must alter the views data and add date plugin.

function MYMODULE_views_data_alter(array &$data) {
  $data['THE_TABLE']['THE_FIELD']['filter']['id'] = 'date';
}

BLOB and TEXT data types.

The long string (string_long) use data type  BLOB or TEXT
If you want to use BLOB, Set case sensitive to true. (Currently I haven't found how to use medium size data fields, if you know WELCOME.)


//Example of BLOB (longblob on MySQL):
$fields['data'] = BaseFieldDefinition::create('string_long')
  ->setLabel(t('Data'))
  ->setSetting('case_sensitive', TRUE);
//
//Example of TEXT (longtext on MySQL):
$fields['data'] = BaseFieldDefinition::create('string_long')
  ->setLabel(t('Data'));

Comments

Felix (not verified)

Fri, 09/15/2017 - 01:47

Great article - really useful info here that is hard to find anywhere else.

Is is possible to define the sort order of the entity_reference in the BaseFieldDefinition? 

If it's not possible in the BaseFieldDefinition then where should I define the sorting?

Add new comment