Comment::baseFieldDefinitions

public static Comment::baseFieldDefinitions(EntityTypeInterface $entity_type)

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\EntityManagerInterface::getFieldDefinitions()

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

File

core/modules/comment/src/Entity/Comment.php, line 210

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['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.'));

  $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', array(
      'type' => 'string_textfield',
      // Default comment body field has weight 20.
      'weight' => 10,
    ))
    ->setDisplayConfigurable('form', TRUE);

  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User ID'))
    ->setDescription(t('The user ID of the comment author.'))
    ->setTranslatable(TRUE)
    ->setSetting('target_type', 'user')
    ->setDefaultValue(0);

  $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)
    // URIs are not length limited by RFC 2616, but we can only store 255
    // characters in our comment DB schema.
    ->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);

  $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['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating whether the comment is published.'))
    ->setTranslatable(TRUE)
    ->setDefaultValue(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'))
    ->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'))
    ->setDescription(t('The field name through which this comment was added.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', FieldStorageConfig::NAME_MAX_LENGTH);

  return $fields;
}
doc_Drupal
2016-10-29 08:50:19
Comments
Leave a Comment

Please login to continue.