I am playing with code to define base fields in a custom entity created in a module.  For example...

class Message extends ContentEntityBase {

  use RevisionLogEntityTrait;

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    // Add the revision metadata fields.
    $fields += static::revisionLogBaseFieldDefinitions($entity_type);

    $fields['title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setRequired(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
      ])
      ->setDisplayConfigurable('view', TRUE);
	  
	$fields['ntitle'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setRequired(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
      ])
      ->setDisplayConfigurable('view', TRUE);
	$fields['mtitle'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setRequired(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
      ])
      ->setDisplayConfigurable('view', TRUE);  
    $fields['content'] = BaseFieldDefinition::create('text_long')
      ->setLabel(t('Content'))
      ->setRequired(TRUE)
      ->setDescription(t('Content of the message'))
      ->setDisplayOptions('form', [
        'type' => 'text_textarea',
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'text_default',
      ])
      ->setDisplayConfigurable('view', TRUE);

    return $fields;
  }

}

I'm particularly interested in the values available for the setDisplayOptions. In the code above, types of text_textarea and string_textfield are used. Can someone point me to the php script/other documentation on drupal.org which defines the values to use for decimal, boolean, integer, date etc. Thanks.

Comments

yolandasmith’s picture

If you are looking for the available properties on Base Field Definition, most of them are documented in the Drupal API pages. They sit under the Typed Data and Entity API sections. I usually check api.drupal.org and browse the Base Field Definition class directly it is all listed there.

peterk900’s picture

I've use your route-map into api.drupal.org to locate BaseFieldDefinition.php where I hoped to find a comment block listing the field plugin names I can use. But it looks like I've not got the right script.  Can you help further?  Thanks.

jaypan’s picture

Looking at this code:

$fields['content'] = BaseFieldDefinition::create('text_long')
  ->setLabel(t('Content'))
  ->setRequired(TRUE)
  ->setDescription(t('Content of the message'))
  ->setDisplayOptions('form', [
    'type' => 'text_textarea',
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'text_default',
  ])
->setDisplayConfigurable('view', TRUE);

When creating a base field on an entity, you pass the plugin ID of the field type to use. In this case, the field type is text_long:

$fields['content'] = BaseFieldDefinition::create('text_long')

Which comes from the plugin TextLongItem

Various widgets (form) and formatters (view) exist that can be used on text_long fields. The relevant widget/formatter are where the options from setDisplayOptions comes from, and you can tell whether it's a widget or a formatter by the first argument. This one is for the widget (form):

->setDisplayOptions('form', [
  'type' => 'text_textarea',
])

So the widget is the FieldWidget plugin with an ID of text_textarea, which is TextareaWidget

And then this is for the formatter (view):

->setDisplayOptions('view', [
  'label' => 'hidden',
  'type' => 'text_default',
])

So the fomatter is the FieldFormatter plugin with an ID of text_default, which is TextDefaultFormatter.

Contact me to contract me for D7 -> D10/11 migrations.

peterk900’s picture

I see now in D8-> it's plugins, not code comments as for D7, that gives this info,

Telling me a plugin name what what I needed to search core and find the folder modules/text/src/Plugin/Field/FieldType/TextLongItem.php

And this folder gives me other text formats:

  • TextFieldItemlist.php
  • TextItem.php
  • TextItemBase.php
  • TextLongItem.php
  • TextWithsummaryItem.php

And the id in the code for each format is the string in the BaseField::Create statement.  

I'm beginning to understand. Thanks.

But all the files under the plugin directory relate to text fields.  What do I do if I want my entity base file to contain other field types e.g. boolean, numeric?  

jaypan’s picture

You can search the web/core and web/modules directories for directories with the hierarchy Plugin/Field/FieldFormatter, or you can search for the string Plugin\Field\FieldFormatter (which will be in the namespace for every formatter) and you can find all the field formatters on the system. Switch FieldWidget if searching for widgets.

Contact me to contract me for D7 -> D10/11 migrations.

peterk900’s picture

This is the information I was after.

BaseFieldDefinition  DisplayOptions         DisplayOptions
::Create                         ('view')                        ('form')  

string                              string                          text_textfield
datetime                        datetime_default      datetime_default
integer                           number_integer       number
float                                number_decimal      number
boolean                          boolean                     boolean_checkbox
timestamp                     timestamp                datetime_timestamp
created                           timestamp

Thank you again for your help with this.