You are here

public static function PaymentMethod::baseFieldDefinitions in Commerce Core 8.2

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

modules/payment/src/Entity/PaymentMethod.php, line 236

Class

PaymentMethod
Defines the payment method entity class.

Namespace

Drupal\commerce_payment\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['payment_gateway'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Payment gateway'))
    ->setDescription(t('The payment gateway.'))
    ->setRequired(TRUE)
    ->setSetting('target_type', 'commerce_payment_gateway');
  $fields['payment_gateway_mode'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Payment gateway mode'))
    ->setDescription(t('The payment gateway mode.'))
    ->setRequired(TRUE);
  $fields['uid']
    ->setLabel(t('Owner'))
    ->setDescription(t('The payment method owner.'))
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'author',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['remote_id'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Remote ID'))
    ->setDescription(t('The payment method remote ID.'))
    ->setSetting('max_length', 255)
    ->setDisplayConfigurable('view', TRUE);
  $fields['billing_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
    ->setLabel(t('Billing profile'))
    ->setDescription(t('Billing profile'))
    ->setSetting('target_type', 'profile')
    ->setSetting('handler', 'default')
    ->setSetting('handler_settings', [
    'target_bundles' => [
      'customer' => 'customer',
    ],
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 0,
    'settings' => [],
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'commerce_payment_method_profile',
    'weight' => 2,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['reusable'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Reusable'))
    ->setDescription(t('Whether the payment method is reusable.'))
    ->setDefaultValue(TRUE)
    ->setDisplayConfigurable('view', TRUE);

  // 'default' is a reserved SQL word, hence the 'is_' prefix.
  $fields['is_default'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Default'))
    ->setDescription(t("Whether this is the user's default payment method."))
    ->setDefaultValue(FALSE);
  $fields['expires'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Expires'))
    ->setDescription(t('The time when the payment method expires. 0 for never.'))
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 1,
    'settings' => [
      'date_format' => 'custom',
      'custom_date_format' => 'n/Y',
    ],
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDefaultValue(0);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time when the payment method was created.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time when the payment method was last edited.'));
  return $fields;
}