You are here

public static function PrivateMessage::baseFieldDefinitions in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Entity/PrivateMessage.php \Drupal\private_message\Entity\PrivateMessage::baseFieldDefinitions()

Provides base field definitions for an entity type.

Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:

$fields['name'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Name'));

By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().

The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.

Overrides ContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

File

src/Entity/PrivateMessage.php, line 101

Class

PrivateMessage
Thee Private Message entity definition.

Namespace

Drupal\private_message\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['id']
    ->setLabel(t('Private message ID'))
    ->setDescription(t('The private message ID.'));
  $fields['uuid']
    ->setDescription(t('The custom private message UUID.'));

  // Owner of the private message.
  // Entity reference field, holds the reference to the user object. The view
  // shows the user name field of the user. No form field is provided, as the
  // user will always be the current user.
  $fields['owner'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('From'))
    ->setDescription(t('The author of the private message'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', [
    'label' => 'inline',
    'type' => 'entity_reference_label',
    'weight' => -3,
  ])
    ->setDisplayConfigurable('view', TRUE);

  // Body of the private message.
  $fields['message'] = BaseFieldDefinition::create('text_long')
    ->setLabel(t('Message'))
    ->setRequired(TRUE)
    ->setCardinality(1)
    ->setTranslatable(FALSE)
    ->setDisplayOptions('form', [
    'type' => 'text_textarea',
    'label' => 'hidden',
    'settings' => [
      'placeholder' => 'Message',
    ],
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'text_textfield',
    'settings' => [
      'trim_length' => '200',
    ],
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the private message was created.'))
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'weight' => -1,
  ])
    ->setDisplayConfigurable('view', TRUE);
  return $fields;
}