You are here

function commerce_product_bundle_add_stores_field in Commerce Product Bundle 8

Adds the default stores field to a product bundle.

A product bundle can belong to multiple stores. Store id can't be a base field because the Views integration is broken. Instead, it is created as a configurable field for each order type.

Parameters

\Drupal\commerce_product_bundle\Entity\BundleTypeInterface $product_bundle_type: The product bundle type.

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

File

./commerce_product_bundle.module, line 235
Contains commerce_product_bundle.module.

Code

function commerce_product_bundle_add_stores_field(BundleTypeInterface $product_bundle_type) {
  $field_definition = BundleFieldDefinition::create('entity_reference')
    ->setTargetEntityTypeId('commerce_product_bundle')
    ->setTargetBundle($product_bundle_type
    ->id())
    ->setName('stores')
    ->setLabel('Stores')
    ->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(FALSE)
    ->setTranslatable(FALSE)
    ->setSetting('target_type', 'commerce_store')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('form', [
    'type' => 'commerce_entity_select',
    'weight' => -10,
  ]);
  $configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
  $configurable_field_manager
    ->createField($field_definition);
}