You are here

public static function Subscription::baseFieldDefinitions in Commerce Recurring Framework 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 774

Class

Subscription
Defines the subscription entity.

Namespace

Drupal\commerce_recurring\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['store_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Store'))
    ->setDescription(t('The store to which the subscription belongs.'))
    ->setCardinality(1)
    ->setRequired(TRUE)
    ->setSetting('target_type', 'commerce_store')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('form', [
    'type' => 'commerce_entity_select',
    'weight' => 2,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['billing_schedule'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Billing schedule'))
    ->setDescription(t('The billing schedule.'))
    ->setRequired(TRUE)
    ->setSetting('target_type', 'commerce_billing_schedule')
    ->setSetting('handler', 'default')
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['uid']
    ->setLabel(t('Customer'))
    ->setDescription(t('The subscribed customer.'))
    ->setRequired(TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['payment_method'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Payment method'))
    ->setDescription(t('The payment method.'))
    ->setSetting('target_type', 'commerce_payment_method')
    ->setDisplayOptions('form', [
    'type' => 'commerce_recurring_payment_method',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setReadOnly(TRUE);
  $fields['purchased_entity'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Purchased entity'))
    ->setDescription(t('The purchased entity.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => -1,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setDescription(t('The subscription title.'))
    ->setRequired(TRUE)
    ->setSettings([
    'default_value' => '',
    'max_length' => 512,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -10,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['quantity'] = BaseFieldDefinition::create('decimal')
    ->setLabel(t('Quantity'))
    ->setDescription(t('The subscription quantity.'))
    ->setRequired(TRUE)
    ->setSetting('unsigned', TRUE)
    ->setSetting('min', 0)
    ->setDefaultValue(1)
    ->setDisplayOptions('form', [
    'type' => 'number',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['unit_price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Unit price'))
    ->setDescription(t('The subscription unit price.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'commerce_price_default',
    'weight' => 2,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['state'] = BaseFieldDefinition::create('state')
    ->setLabel(t('State'))
    ->setDescription(t('The subscription state.'))
    ->setRequired(TRUE)
    ->setSetting('max_length', 255)
    ->setSetting('workflow', 'subscription_default')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'list_default',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['initial_order'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Initial order'))
    ->setDescription(t('The non-recurring order which started the subscription.'))
    ->setSetting('target_type', 'commerce_order')
    ->setSetting('handler', 'default')
    ->setSetting('display_description', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['orders'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Recurring orders'))
    ->setDescription(t('The recurring orders.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setSetting('target_type', 'commerce_order')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', [
    'type' => 'subscription_orders',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time when the subscription was created.'))
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['next_renewal'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Next renewal'))
    ->setDescription(t('The next renewal time.'))
    ->setDefaultValue(0)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['renewed'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Last renewed'))
    ->setDescription(t('The time when the subscription was last renewed.'))
    ->setDefaultValue(0)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['trial_starts'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Trial starts'))
    ->setDescription(t('The time when the subscription trial starts.'))
    ->setRequired(FALSE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['trial_ends'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Trial ends'))
    ->setDescription(t('The time when the subscription trial ends.'))
    ->setRequired(FALSE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'commerce_recurring_end_timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['starts'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Starts'))
    ->setDescription(t('The time when the subscription starts.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['ends'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Ends'))
    ->setDescription(t('The time when the subscription ends.'))
    ->setRequired(FALSE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'commerce_recurring_end_timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['scheduled_changes'] = BaseFieldDefinition::create('commerce_scheduled_change')
    ->setLabel(t('Scheduled changes'))
    ->setRequired(FALSE)
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'commerce_scheduled_change_default',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', TRUE);
  return $fields;
}