You are here

public static function PriceListItem::baseFieldDefinitions in Commerce Pricelist 8.2

Same name and namespace in other branches
  1. 8 src/Entity/PriceListItem.php \Drupal\commerce_pricelist\Entity\PriceListItem::baseFieldDefinitions()

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/PriceListItem.php, line 231

Class

PriceListItem
Defines the price list item entity.

Namespace

Drupal\commerce_pricelist\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['uid']
    ->setLabel(t('Owner'))
    ->setDescription(t('The user that owns this price list item.'));
  $fields['price_list_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Price list'))
    ->setDescription(t('The parent price list.'))
    ->setSetting('target_type', 'commerce_pricelist')
    ->setDisplayConfigurable('form', TRUE)
    ->setRequired(TRUE)
    ->setReadOnly(TRUE);
  $fields['purchasable_entity'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Purchasable entity'))
    ->setDescription(t('The purchasable entity.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => -1,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ]);

  // Provide a default target_type for Views, which uses
  // base field definitions without bundle overrides.
  if (\Drupal::moduleHandler()
    ->moduleExists('commerce_product')) {
    $fields['purchasable_entity']
      ->setSetting('target_type', 'commerce_product_variation');
  }
  $fields['quantity'] = BaseFieldDefinition::create('decimal')
    ->setLabel(t('Quantity'))
    ->setDescription(t('The quantity tier.'))
    ->setRequired(TRUE)
    ->setSetting('unsigned', TRUE)
    ->setSetting('min', 0)
    ->setDefaultValue(1)
    ->setDisplayOptions('form', [
    'type' => 'commerce_quantity',
  ]);
  $fields['list_price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('List price'))
    ->setDescription(t('The list price.'))
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'commerce_price_default',
  ])
    ->setDisplayOptions('form', [
    'type' => 'commerce_list_price',
  ]);
  $fields['price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Price'))
    ->setDescription(t('The price.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'commerce_price_default',
  ])
    ->setDisplayOptions('form', [
    'type' => 'commerce_price_default',
  ]);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Status'))
    ->setDescription(t('Whether the price list item is enabled.'))
    ->setDefaultValue(TRUE)
    ->setRequired(TRUE)
    ->setSettings([
    'on_label' => t('Enabled'),
    'off_label' => t('Disabled'),
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
  ]);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time when the price list item was last edited.'))
    ->setTranslatable(TRUE);
  return $fields;
}