You are here

public static function Subscriber::baseFieldDefinitions in Simplenews 8.2

Same name and namespace in other branches
  1. 8 src/Entity/Subscriber.php \Drupal\simplenews\Entity\Subscriber::baseFieldDefinitions()
  2. 3.x src/Entity/Subscriber.php \Drupal\simplenews\Entity\Subscriber::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

src/Entity/Subscriber.php, line 339

Class

Subscriber
Defines the simplenews subscriber entity.

Namespace

Drupal\simplenews\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Subscriber ID'))
    ->setDescription(t('Primary key: Unique subscriber ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The subscriber UUID.'))
    ->setReadOnly(TRUE);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Status'))
    ->setDescription(t('Boolean indicating the status of the subscriber.'))
    ->setDefaultValue(TRUE);
  $fields['mail'] = BaseFieldDefinition::create('email')
    ->setLabel(t('Email'))
    ->setDescription(t("The subscriber's email address."))
    ->setSetting('default_value', '')
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'email_default',
    'settings' => [],
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User'))
    ->setDescription(t('The corresponding user.'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default');
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language'))
    ->setDescription(t("The subscriber's preferred language."));
  $fields['changes'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Changes'))
    ->setDescription(t('Contains the requested subscription changes.'));
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the subscriber was created.'));
  $fields['subscriptions'] = BaseFieldDefinition::create('simplenews_subscription')
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setLabel(t('Subscriptions'))
    ->setSetting('target_type', 'simplenews_newsletter')
    ->setDisplayOptions('form', [
    'type' => 'simplenews_subscription_select',
    'weight' => '0',
    'settings' => [],
    'third_party_settings' => [],
  ]);
  return $fields;
}