You are here

public static function BundleItemOrderItem::baseFieldDefinitions in Commerce Product Bundle 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/BundleItemOrderItem.php, line 167

Class

BundleItemOrderItem
Defines the Bundle Item Order Item entity.

Namespace

Drupal\commerce_product_bundle\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setDescription(t('The title of the Bundle Item Order Item entity.'))
    ->setSettings([
    'max_length' => 512,
    'text_processing' => 0,
  ])
    ->setDefaultValue('')
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'string',
    'weight' => -4,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  // The order item backreference, populated by XXX.
  $fields['order_item_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Order Item'))
    ->setDescription(t('The parent order item.'))
    ->setSetting('target_type', 'commerce_order_item')
    ->setCardinality(1)
    ->setReadOnly(TRUE);
  $fields['purchased_entity'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Purchased entity'))
    ->setDescription(t('The purchased entity.'))
    ->setSetting('target_type', 'commerce_product_variation')
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => -1,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
      'match_limit' => 10,
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['bundle_item'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Bundle item'))
    ->setDescription(t('The bundle item the purchased entity belongs to.'))
    ->setSetting('target_type', 'commerce_product_bundle_i')
    ->setRequired(FALSE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => -1,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'match_limit' => 10,
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['quantity'] = BaseFieldDefinition::create('decimal')
    ->setLabel(t('Quantity'))
    ->setDescription(t('The number of purchased units.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE)
    ->setSetting('min', 0)
    ->setDefaultValue(1)
    ->setDisplayOptions('form', [
    'type' => 'commerce_quantity',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['unit_price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Unit price'))
    ->setDescription(t('The price of a single unit.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'commerce_unit_price',
    'weight' => 2,
    'settings' => [
      'require_confirmation' => TRUE,
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['total_price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Total price'))
    ->setDescription(t('The total price of the bundle item order item.'))
    ->setReadOnly(TRUE)
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time when the bundle item order item was created.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time when the bundle item order item was last edited.'))
    ->setRequired(TRUE);
  return $fields;
}