You are here

public static function Comment::baseFieldDefinitions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/src/Entity/Comment.php \Drupal\comment\Entity\Comment::baseFieldDefinitions()
  2. 10 core/modules/comment/src/Entity/Comment.php \Drupal\comment\Entity\Comment::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/comment/src/Entity/Comment.php, line 224

Class

Comment
Defines the comment entity class.

Namespace

Drupal\comment\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::publishedBaseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['cid']
    ->setLabel(t('Comment ID'))
    ->setDescription(t('The comment ID.'));
  $fields['uuid']
    ->setDescription(t('The comment UUID.'));
  $fields['comment_type']
    ->setLabel(t('Comment Type'))
    ->setDescription(t('The comment type.'));
  $fields['langcode']
    ->setDescription(t('The comment language code.'));

  // Set the default value callback for the status field.
  $fields['status']
    ->setDefaultValueCallback('Drupal\\comment\\Entity\\Comment::getDefaultStatus');
  $fields['pid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Parent ID'))
    ->setDescription(t('The parent comment ID if this is a reply to a comment.'))
    ->setSetting('target_type', 'comment');
  $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Entity ID'))
    ->setDescription(t('The ID of the entity of which this comment is a reply.'))
    ->setRequired(TRUE);
  $fields['subject'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Subject'))
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 64)
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    // Default comment body field has weight 20.
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['uid']
    ->setDescription(t('The user ID of the comment author.'));
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t("The comment author's name."))
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 60)
    ->setDefaultValue('');
  $fields['mail'] = BaseFieldDefinition::create('email')
    ->setLabel(t('Email'))
    ->setDescription(t("The comment author's email address."))
    ->setTranslatable(TRUE);
  $fields['homepage'] = BaseFieldDefinition::create('uri')
    ->setLabel(t('Homepage'))
    ->setDescription(t("The comment author's home page address."))
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 255);
  $fields['hostname'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Hostname'))
    ->setDescription(t("The comment author's hostname."))
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 128)
    ->setDefaultValueCallback(static::class . '::getDefaultHostname');
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the comment was created.'))
    ->setTranslatable(TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the comment was last edited.'))
    ->setTranslatable(TRUE);
  $fields['thread'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Thread place'))
    ->setDescription(t("The alphadecimal representation of the comment's place in a thread, consisting of a base 36 string prefixed by an integer indicating its length."))
    ->setSetting('max_length', 255);
  $fields['entity_type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Entity type'))
    ->setRequired(TRUE)
    ->setDescription(t('The entity type to which this comment is attached.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);
  $fields['field_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Comment field name'))
    ->setRequired(TRUE)
    ->setDescription(t('The field name through which this comment was added.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', FieldStorageConfig::NAME_MAX_LENGTH);
  return $fields;
}