public static function OrderItem::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/
order/ src/ Entity/ OrderItem.php, line 369
Class
- OrderItem
- Defines the order item entity class.
Namespace
Drupal\commerce_order\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// The order backreference, populated by Order::postSave().
$fields['order_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Order'))
->setDescription(t('The parent order.'))
->setSetting('target_type', 'commerce_order')
->setReadOnly(TRUE);
$fields['purchased_entity'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Purchased entity'))
->setDescription(t('The purchased entity.'))
->setRequired(TRUE)
->addConstraint('PurchasedEntityAvailable')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -1,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
/** @var \Drupal\commerce\PurchasableEntityTypeRepositoryInterface $purchasable_entity_type_repository */
$purchasable_entity_type_repository = \Drupal::service('commerce.purchasable_entity_type_repository');
$default_purchasable_entity_type = $purchasable_entity_type_repository
->getDefaultPurchasableEntityType();
if ($default_purchasable_entity_type) {
$fields['purchased_entity']
->setSetting('target_type', $default_purchasable_entity_type
->id());
}
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The order item title.'))
->setSettings([
'default_value' => '',
'max_length' => 512,
]);
$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['overridden_unit_price'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Overridden unit price'))
->setDescription(t('Whether the unit price is overridden.'))
->setDefaultValue(FALSE);
$fields['total_price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Total price'))
->setDescription(t('The total price of the order item.'))
->setReadOnly(TRUE)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', TRUE);
$fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
->setLabel(t('Adjustments'))
->setRequired(FALSE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', TRUE);
$fields['uses_legacy_adjustments'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Uses legacy adjustments'))
->setSettings([
'on_label' => t('Yes'),
'off_label' => t('No'),
])
->setDefaultValue(FALSE);
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Data'))
->setDescription(t('A serialized array of additional data.'));
$fields['locked'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Locked'))
->setSettings([
'on_label' => t('Yes'),
'off_label' => t('No'),
])
->setDefaultValue(FALSE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the order item was created.'))
->setRequired(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the order item was last edited.'))
->setRequired(TRUE);
return $fields;
}