BundleItemSelection.php in Commerce Product Bundle 8
File
src/Plugin/Field/FieldType/BundleItemSelection.php
View source
<?php
namespace Drupal\commerce_product_bundle\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
class BundleItemSelection extends FieldItemBase {
protected $variationStorage;
protected $bundleItemStorage;
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
$this->variationStorage = \Drupal::service('entity_type.manager')
->getStorage('commerce_product_variation');
$this->bundleItemStorage = \Drupal::service('entity_type.manager')
->getStorage('commerce_product_bundle_i');
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['bundle_item'] = DataDefinition::create('string')
->setLabel(t('Bundle item'))
->setRequired(FALSE);
$properties['selected_qty'] = DataDefinition::create('string')
->setLabel(t('Selected quantity'))
->setRequired(FALSE);
$properties['selected_entity'] = DataDefinition::create('string')
->setLabel(t('Selected entity'))
->setRequired(FALSE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'bundle_item' => [
'description' => 'The bundle item.',
'type' => 'numeric',
'precision' => 19,
'scale' => 0,
],
'selected_qty' => [
'description' => 'The selected quantity.',
'type' => 'numeric',
'precision' => 17,
'scale' => 2,
],
'selected_entity' => [
'description' => 'The selected entity id.',
'type' => 'numeric',
'precision' => 19,
'scale' => 0,
],
],
];
}
public static function mainPropertyName() {
return NULL;
}
public function isEmpty() {
return empty($this->bundle_item) || empty($this->selected_qty) || empty($this->selected_entity);
}
public function setValue($values, $notify = TRUE) {
$bundleItem = $this->bundleItemStorage
->load($values['bundle_item']);
$purchasableEntity = $this->variationStorage
->load($values['purchasable_entity']);
$bundleItem
->setCurrentVariation($purchasableEntity);
parent::setValue($values, $notify);
}
}
Classes
Name |
Description |
BundleItemSelection |
Plugin implementation of the 'commerce_product_bundle_item_selection' field type. |