You are here

public static function Promotion::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/promotion/src/Entity/Promotion.php, line 680

Class

Promotion
Defines the promotion entity class.

Namespace

Drupal\commerce_promotion\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t('The promotion name.'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setSettings([
    'default_value' => '',
    'max_length' => 255,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['uid']
    ->setLabel(t('Owner'))
    ->setDescription(t('The promotion owner.'))
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['display_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Display name'))
    ->setDescription(t('If provided, shown on the order instead of "@translated".', [
    '@translated' => t('Discount'),
  ]))
    ->setTranslatable(TRUE)
    ->setSettings([
    'display_description' => TRUE,
    'default_value' => '',
    'max_length' => 255,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['description'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Description'))
    ->setDescription(t('Additional information about the promotion to show to the customer'))
    ->setTranslatable(TRUE)
    ->setDefaultValue('')
    ->setDisplayOptions('form', [
    'type' => 'string_textarea',
    'weight' => 1,
    'settings' => [
      'rows' => 3,
    ],
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setTranslatable(TRUE)
    ->setDescription(t('The time when the promotion was created.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setTranslatable(TRUE)
    ->setDescription(t('The time when the promotion was last edited.'))
    ->setDisplayConfigurable('view', TRUE);
  $fields['order_types'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Order types'))
    ->setDescription(t('The order types for which the promotion is valid.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(TRUE)
    ->setSetting('target_type', 'commerce_order_type')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('form', [
    'type' => 'commerce_entity_select',
    'weight' => 2,
  ]);
  $fields['stores'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Stores'))
    ->setDescription(t('The stores for which the promotion is valid.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(TRUE)
    ->setSetting('target_type', 'commerce_store')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('form', [
    'type' => 'commerce_entity_select',
    'weight' => 2,
  ]);
  $fields['offer'] = BaseFieldDefinition::create('commerce_plugin_item:commerce_promotion_offer')
    ->setLabel(t('Offer type'))
    ->setCardinality(1)
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'commerce_plugin_select',
    'weight' => 3,
  ]);
  $fields['conditions'] = BaseFieldDefinition::create('commerce_plugin_item:commerce_condition')
    ->setLabel(t('Conditions'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(FALSE)
    ->setDisplayOptions('form', [
    'type' => 'commerce_conditions',
    'weight' => 3,
    'settings' => [
      'entity_types' => [
        'commerce_order',
      ],
    ],
  ]);
  $fields['condition_operator'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Condition operator'))
    ->setDescription(t('The condition operator.'))
    ->setRequired(TRUE)
    ->setSetting('allowed_values', [
    'AND' => t('All conditions must pass'),
    'OR' => t('Only one condition must pass'),
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => 4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDefaultValue('AND');
  $fields['coupons'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Coupons'))
    ->setDescription(t('Coupons which allow promotion to be redeemed.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(FALSE)
    ->setSetting('target_type', 'commerce_promotion_coupon')
    ->setSetting('handler', 'default');
  $fields['usage_limit'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Usage limit'))
    ->setDescription(t('The maximum number of times the promotion can be used. 0 for unlimited.'))
    ->setDefaultValue(0)
    ->setDisplayOptions('form', [
    'type' => 'commerce_usage_limit',
    'weight' => 4,
  ]);
  $fields['usage_limit_customer'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Customer usage limit'))
    ->setDescription(t('The maximum number of times the promotion can be used by a customer. 0 for unlimited.'))
    ->setDefaultValue(0)
    ->setDisplayOptions('form', [
    'type' => 'commerce_usage_limit',
    'weight' => 4,
  ]);
  $fields['start_date'] = BaseFieldDefinition::create('datetime')
    ->setLabel(t('Start date'))
    ->setDescription(t('The date the promotion becomes valid.'))
    ->setRequired(TRUE)
    ->setSetting('datetime_type', 'datetime')
    ->setDefaultValueCallback('Drupal\\commerce_promotion\\Entity\\Promotion::getDefaultStartDate')
    ->setDisplayOptions('form', [
    'type' => 'commerce_store_datetime',
    'weight' => 5,
  ]);
  $fields['end_date'] = BaseFieldDefinition::create('datetime')
    ->setLabel(t('End date'))
    ->setDescription(t('The date after which the promotion is invalid.'))
    ->setRequired(FALSE)
    ->setSetting('datetime_type', 'datetime')
    ->setSetting('datetime_optional_label', t('Provide an end date'))
    ->setDisplayOptions('form', [
    'type' => 'commerce_store_datetime',
    'weight' => 6,
  ]);
  $fields['compatibility'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Compatibility with other promotions'))
    ->setSetting('allowed_values_function', [
    '\\Drupal\\commerce_promotion\\Entity\\Promotion',
    'getCompatibilityOptions',
  ])
    ->setRequired(TRUE)
    ->setDefaultValue(self::COMPATIBLE_ANY)
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => 4,
  ]);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Status'))
    ->setDescription(t('Whether the promotion is enabled.'))
    ->setDefaultValue(TRUE)
    ->setRequired(TRUE)
    ->setSettings([
    'on_label' => t('Enabled'),
    'off_label' => t('Disabled'),
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => 0,
  ]);
  $fields['weight'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Weight'))
    ->setDescription(t('The weight of this promotion in relation to others.'))
    ->setDefaultValue(0);
  return $fields;
}