public static function Subscription::baseFieldDefinitions in Mailing List 8
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/ Subscription.php, line 245
Class
- Subscription
- Defines the subscription entity class.
Namespace
Drupal\mailing_list\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// Generic base fields.
$fields = parent::baseFieldDefinitions($entity_type);
// Subscription ID custom label & description.
$fields['sid']
->setLabel(t('Subscription ID'))
->setDescription(t('The ID of the subscription.'));
// Bundle field custom description.
$fields['mailing_list']
->setDescription(t('The mailing list to which this subscription belongs.'));
// User ID (subscription author/owner).
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setRequired(TRUE)
->setDescription(t('The user ID of the subscriber.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\mailing_list\\Entity\\Subscription::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE);
// Subscription title or name.
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE);
// Subscription email address.
$fields['email'] = BaseFieldDefinition::create('email')
->setLabel(t('Email'))
->setDescription(t('The email address of this subscription.'))
->setRequired(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'type' => 'string',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'email_default',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
// Subscription status (active/inactive).
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Active'))
->setDescription(t('Indicates whether the subscription is active.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(SubscriptionInterface::ACTIVE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'title' => t('Subscription status'),
'label' => 'above',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE);
// Subscription creation date.
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Added on'))
->setDescription(t('The time that the subscription was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
// Subscription last changed time.
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the subscription was last updated.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}