You are here

function commerce_product_bundle_add_variations_field in Commerce Product Bundle 8

Adds the default variations field to a bundle item type.

Variations can't be a base field because the Views integration is broken. Instead, it is created as a configurable field for each bundle item type.

@todo Consider how this may change.

Parameters

\Drupal\commerce_product_bundle\Entity\BundleItemTypeInterface $bundle_item_type: The bundle item type the field should be attached to.

See also

https://www.drupal.org/node/2837499

1 call to commerce_product_bundle_add_variations_field()
ProductBundleItemTypeForm::save in src/Form/ProductBundleItemTypeForm.php
Form submission handler for the 'save' action.

File

./commerce_product_bundle.module, line 201
Contains commerce_product_bundle.module.

Code

function commerce_product_bundle_add_variations_field(BundleItemTypeInterface $bundle_item_type) {
  $field_definition = BundleFieldDefinition::create('entity_reference')
    ->setTargetEntityTypeId('commerce_product_bundle_i')
    ->setTargetBundle($bundle_item_type
    ->id())
    ->setName('variations')
    ->setLabel('Variations')
    ->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(FALSE)
    ->setTranslatable(FALSE)
    ->setSetting('target_type', 'commerce_product_variation')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => 10,
  ])
    ->setDisplayOptions('view', [
    'type' => 'commerce_add_to_cart',
    'weight' => 10,
  ]);
  $configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
  $configurable_field_manager
    ->createField($field_definition);
}