You are here

public static function Message::baseFieldDefinitions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/contact/src/Entity/Message.php \Drupal\contact\Entity\Message::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

core/modules/contact/src/Entity/Message.php, line 140

Class

Message
Defines the contact message entity.

Namespace

Drupal\contact\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['contact_form']
    ->setLabel(t('Form ID'))
    ->setDescription(t('The ID of the associated form.'));
  $fields['uuid']
    ->setDescription(t('The message UUID.'));
  $fields['langcode']
    ->setDescription(t('The message language code.'));
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t("The sender's name"))
    ->setDescription(t('The name of the person that is sending the contact message.'));
  $fields['mail'] = BaseFieldDefinition::create('email')
    ->setLabel(t("The sender's email"))
    ->setDescription(t('The email of the person that is sending the contact message.'));

  // The subject of the contact message.
  $fields['subject'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Subject'))
    ->setRequired(TRUE)
    ->setSetting('max_length', 100)
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -10,
  ])
    ->setDisplayConfigurable('form', TRUE);

  // The text of the contact message.
  $fields['message'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Message'))
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'string_textarea',
    'weight' => 0,
    'settings' => [
      'rows' => 12,
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('view', [
    'type' => 'string',
    'weight' => 0,
    'label' => 'above',
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['copy'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Copy'))
    ->setDescription(t('Whether to send a copy of the message to the sender.'));
  $fields['recipient'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Recipient ID'))
    ->setDescription(t('The ID of the recipient user for personal contact messages.'))
    ->setSetting('target_type', 'user');
  return $fields;
}