public static function ProductVariation::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/
product/ src/ Entity/ ProductVariation.php, line 443
Class
- ProductVariation
- Defines the product variation entity class.
Namespace
Drupal\commerce_product\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields['uid']
->setLabel(t('Author'))
->setDescription(t('The variation author.'))
->setDisplayConfigurable('form', TRUE);
// The product backreference, populated by Product::postSave().
$fields['product_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Product'))
->setDescription(t('The parent product.'))
->setSetting('target_type', 'commerce_product')
->setReadOnly(TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['sku'] = BaseFieldDefinition::create('string')
->setLabel(t('SKU'))
->setDescription(t('The unique, machine-readable identifier for a variation.'))
->setRequired(TRUE)
->addConstraint('ProductVariationSku')
->setSetting('display_description', TRUE)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The variation title.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setSettings([
'default_value' => '',
'max_length' => 255,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['list_price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('List price'))
->setDescription(t('The list price.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'commerce_price_default',
'weight' => -1,
])
->setDisplayOptions('form', [
'type' => 'commerce_list_price',
'weight' => -1,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Price'))
->setDescription(t('The price'))
->setRequired(TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'commerce_price_default',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'commerce_price_default',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status']
->setLabel(t('Published'))
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 90,
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the variation was created.'))
->setTranslatable(TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the variation was last edited.'))
->setTranslatable(TRUE);
return $fields;
}