public static function Flagging::baseFieldDefinitions in Flag 8.4
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/ Flagging.php, line 109
Class
- Flagging
- Provides the flagging content entity.
Namespace
Drupal\flag\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// Add descriptions to the fields defined by the parent method.
$fields['id']
->setDescription(t('The flagging ID.'));
$fields['flag_id']
->setDescription(t('The Flag ID.'));
// This field is on flaggings even though it duplicates the entity type
// field on the flag so that flagging queries can use it.
$fields['entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Entity Type'))
->setDescription(t('The Entity Type.'));
$fields['entity_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Entity ID'))
->setRequired(TRUE)
->setDescription(t('The Entity ID.'));
$fields['flagged_entity'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Entity'))
->setDescription(t('The flagged entity.'))
->setComputed(TRUE)
->setClass(FlaggedEntityFieldItemList::class);
// Also duplicates data on flag entity for querying purposes.
$fields['global'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Global'))
->setDescription(t('A boolean indicating whether the flagging is global.'));
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID of the flagging user. This is recorded for both global and personal flags.'))
->setSettings([
'target_type' => 'user',
'default_value' => 0,
]);
$fields['session_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Session ID'))
->setDescription(t('The session ID associated with an anonymous user.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the flagging was created.'));
return $fields;
}