BundleItemSelectionDefaultWidget.php in Commerce Product Bundle 8
File
src/Plugin/Field/FieldWidget/BundleItemSelectionDefaultWidget.php
View source
<?php
namespace Drupal\commerce_product_bundle\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class BundleItemSelectionDefaultWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$variationStorage = \Drupal::service('entity_type.manager')
->getStorage('commerce_product_variation');
$bundleItemStorage = \Drupal::service('entity_type.manager')
->getStorage('commerce_product_bundle_i');
$bundle_item = $items[$delta]->bundle_item;
$selected_qty = $items[$delta]->selected_qty;
$selected_entity = $items[$delta]->selected_entity;
$element['bundle_item'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Bundle item'),
'#weight' => 0,
'#target_type' => 'commerce_product_bundle_i',
'#default_value' => $bundle_item ? $bundleItemStorage
->load($bundle_item) : NULL,
];
$element['selected_entity'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Selected entity'),
'#weight' => 2,
'#target_type' => 'commerce_product_variation',
'#default_value' => $selected_entity ? $variationStorage
->load($selected_entity) : NULL,
];
$element['selected_qty'] = [
'#type' => 'textfield',
'#title' => $this
->t('Selected quantity'),
'#weight' => 1,
'#default_value' => $selected_qty,
];
return $element;
}
}