You are here

public static function Vote::baseFieldDefinitions in Voting API 8.3

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/Vote.php, line 168

Class

Vote
Defines the Vote entity.

Namespace

Drupal\votingapi\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('ID'))
    ->setDescription(t('The vote ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The vote UUID.'))
    ->setReadOnly(TRUE);
  $fields['type'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Type'))
    ->setDescription(t('The vote type.'))
    ->setSetting('target_type', 'vote_type')
    ->setReadOnly(TRUE);
  $fields['entity_type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Entity Type'))
    ->setDescription(t('The type from the voted entity.'))
    ->setDefaultValue('node')
    ->setSettings([
    'max_length' => 64,
  ])
    ->setRequired(TRUE);
  $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Voted entity'))
    ->setDescription(t('The ID from the voted entity'))
    ->setDefaultValue(0)
    ->setRequired(TRUE);
  $fields['value'] = BaseFieldDefinition::create('float')
    ->setLabel(t('Value'))
    ->setDescription(t('The numeric value of the vote.'))
    ->setDefaultValue(0)
    ->setRequired(TRUE);
  $fields['value_type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Value Type'))
    ->setSettings([
    'max_length' => 64,
  ])
    ->setDefaultValue('percent');
  $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The user who submitted the vote.'))
    ->setSetting('target_type', 'user')
    ->setDefaultValueCallback('Drupal\\votingapi\\Entity\\Vote::getCurrentUserId');
  $fields['timestamp'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the entity was created.'));
  $fields['vote_source'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Vote Source'))
    ->setDescription(t('The IP address hash from the user who submitted the vote.'))
    ->setDefaultValueCallback('Drupal\\votingapi\\Entity\\Vote::getCurrentIp')
    ->setSettings([
    'max_length' => 255,
  ]);
  return $fields;
}