You are here

public static function ProductBundleItem::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/ProductBundleItem.php, line 514

Class

ProductBundleItem
Defines the product bundle item entity.

Namespace

Drupal\commerce_product_bundle\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Author'))
    ->setDescription(t('The user ID of author of the product bundle item entity.'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValueCallback('Drupal\\commerce_product_bundle\\Entity\\ProductBundleItem::getCurrentUserId')
    ->setDisplayOptions('view', [
    'title' => 'hidden',
    'type' => 'author',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'match_limit' => 10,
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setDescription(t('The title of the product bundle item entity.'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setSettings([
    'max_length' => 128,
    'text_processing' => 0,
  ])
    ->setDefaultValue('')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -4,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['required'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Required?'))
    ->setDescription(t('A boolean indicating whether the product bundle item is required or optional.'))
    ->setSettings([
    'on_label' => t('Yes, required'),
    'off_label' => t('No, optional'),
  ])
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 20,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDefaultValue(TRUE);

  // The product bundle backreference, populated by ProductBundle::postSave().
  $fields['bundle_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Product bundle'))
    ->setDescription(t('The parent product bundle.'))
    ->setSetting('target_type', 'commerce_product_bundle')
    ->setReadOnly(TRUE)
    ->setDisplayConfigurable('view', TRUE);

  // The price is not required because it's not guaranteed to be used
  // for storage. We may use the price of the referenced variations.
  // entity.
  $fields['unit_price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Unit price'))
    ->setDescription(t('The unit price, if overridden, of the variation selected from this bundle item.'))
    ->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['product'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Product'))
    ->setDescription(t('Reference to a product.'))
    ->setSetting('target_type', 'commerce_product')
    ->setSetting('handler', 'default')
    ->setCardinality(1)
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'entity_reference_label',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'match_limit' => 10,
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  // Variations added in commerce_product_bundle.module.
  // @see commerce_product_bundle_add_variations_field().
  $fields['min_quantity'] = BaseFieldDefinition::create('decimal')
    ->setLabel(t('Minimum Quantity'))
    ->setDescription(t('The minimum quantity.'))
    ->setSetting('unsigned', TRUE)
    ->setSetting('min', 0)
    ->setRequired(TRUE)
    ->setDefaultValue(1)
    ->setDisplayOptions('form', [
    'type' => 'number',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->addPropertyConstraints('value', [
    'Range' => [
      'min' => 0,
    ],
  ]);
  $fields['max_quantity'] = BaseFieldDefinition::create('decimal')
    ->setLabel(t('Maximum Quantity'))
    ->setDescription(t('The maximum quantity.'))
    ->setSetting('unsigned', TRUE)
    ->setSetting('min', 1)
    ->setRequired(TRUE)
    ->setDefaultValue(1)
    ->setDisplayOptions('form', [
    'type' => 'number',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the entity was created.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the entity was last edited.'));
  return $fields;
}