You are here

function commerce_product_bundle_add_items_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\BundleTypeInterface $bundle_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_items_field()
ProductBundleTypeForm::save in src/Form/ProductBundleTypeForm.php
Form submission handler for the 'save' action.

File

./commerce_product_bundle.module, line 156
Contains commerce_product_bundle.module.

Code

function commerce_product_bundle_add_items_field(BundleTypeInterface $bundle_type) {
  $field_definition = BundleFieldDefinition::create('entity_reference')
    ->setTargetEntityTypeId('commerce_product_bundle')
    ->setTargetBundle($bundle_type
    ->id())
    ->setName('bundle_items')
    ->setLabel('Bundle items')
    ->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(FALSE)
    ->setTranslatable(FALSE)
    ->setSetting('target_type', 'commerce_product_bundle_i')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 10,
    'settings' => [
      'override_labels' => TRUE,
      'label_singular' => 'bundle item',
      'label_plural' => 'bundle items',
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
      'match_limit' => 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);
}