You are here

public static function PrivateMessageThread::baseFieldDefinitions in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Entity/PrivateMessageThread.php \Drupal\private_message\Entity\PrivateMessageThread::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()

1 call to PrivateMessageThread::baseFieldDefinitions()
PrivateMessageForm::validateMembers in src/Form/PrivateMessageForm.php
Validate members that have been added to a private message thread.

File

src/Entity/PrivateMessageThread.php, line 351

Class

PrivateMessageThread
Defines the Private Message Thread entity.

Namespace

Drupal\private_message\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entityType) {
  $fields = parent::baseFieldDefinitions($entityType);
  $fields['id']
    ->setLabel(t('Private message thread ID'))
    ->setDescription(t('The private message thread ID.'));
  $fields['uuid']
    ->setDescription(t('The custom private message thread UUID.'));
  $fields['updated'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Updated'))
    ->setDescription(t('The most recent time at which the thread was updated'));

  // Member(s) of the private message thread.
  // Entity reference field, holds the reference to user objects.
  $fields['members'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Member(s)'))
    ->setDescription(t('The member(s) of the private message thread'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->addConstraint('private_message_thread_member')
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'entity_reference_label',
    'weight' => -3,
  ])
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ],
    'weight' => -3,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  // Private messages in this thread.
  // Entity reference field, holds the reference to user objects.
  $fields['private_messages'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Messages'))
    ->setDescription(t('The private messages that belong to this thread'))
    ->setSetting('target_type', 'private_message')
    ->setSetting('handler', 'default')
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'entity_reference_entity_view',
  ])
    ->setDisplayConfigurable('view', TRUE);

  // Last access
  // Timestamps for each member, representing the last time at which they
  // accessed the private message thread.
  $fields['last_access_time'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Last Access Times'))
    ->setDescription(t('Timestamps at which members of this private message thread last accessed the thread'))
    ->setSetting('target_type', 'pm_thread_access_time')
    ->setSetting('handler', 'default')
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

  // Last delete
  // Timestamps for each member, representing the last time at which they
  // deleted the private message thread. Note that the thread is only deleted
  // from the database if/when all members have deleted the thread. Until that
  // point, this value is used to determine the first message to be shown to a
  // user if they have deleted the thread, then re-entered a conversation with
  // the other members of the thread.
  $fields['last_delete_time'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Last Delete Times'))
    ->setDescription(t('Timestamps at which members of this private message thread last deleted the thread'))
    ->setSetting('target_type', 'pm_thread_delete_time')
    ->setSetting('handler', 'default')
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  return $fields;
}