View source
<?php
namespace Drupal\commerce_product_bundle\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\commerce_price\Price;
use Drupal\user\UserInterface;
class ProductBundle extends ContentEntityBase implements BundleInterface {
use EntityChangedTrait;
public function getType() {
return $this
->bundle();
}
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function isPublished() {
return (bool) $this
->getEntityKey('status');
}
public function setPublished($published) {
$this
->set('status', $published ? TRUE : FALSE);
return $this;
}
public function getStores() {
return $this
->get('stores')
->referencedEntities();
}
public function getOrderItemTypeId() {
$type_storage = $this
->entityTypeManager()
->getStorage('commerce_product_bundle_type');
$type_entity = $type_storage
->load($this
->bundle());
return $type_entity
->getOrderItemTypeId();
}
public function getOrderItemTitle() {
return $this
->label();
}
public function setPrice(Price $price) {
$this
->set('bundle_price', $price);
return $this;
}
public function getPrice() {
if ($this
->get('bundle_price')
->isEmpty()) {
return NULL;
}
return $this
->get('bundle_price')
->first()
->toPrice();
}
public function getBundleItems() {
$bundle_items = $this
->get('bundle_items')
->referencedEntities();
return $this
->ensureTranslations($bundle_items);
}
public function setBundleItems(array $bundle_items) {
$this
->set('bundle_items', $bundle_items);
return $this;
}
public function addBundleItem(BundleItemInterface $bundle_item) {
if (!$this
->hasBundleItem($bundle_item)) {
$this
->get('bundle_items')
->appendItem($bundle_item);
}
return $this;
}
public function removeBundleItem(BundleItemInterface $bundle_item) {
$index = $this
->getBundleItemIndex($bundle_item);
if ($index !== FALSE) {
$this
->get('bundle_items')
->offsetUnset($index);
}
return $this;
}
protected function getBundleItemIndex(BundleItemInterface $bundle_item) {
return array_search($bundle_item
->id(), $this
->getBundleItemIds());
}
public function hasBundleItems() {
return !$this
->get('bundle_items')
->isEmpty();
}
public function hasBundleItem(BundleItemInterface $bundle_item) {
return in_array($bundle_item
->id(), $this
->getBundleItemIds());
}
public function getBundleItemIds() {
$item_ids = [];
foreach ($this
->get('bundle_items') as $field_item) {
$item_ids[] = $field_item->target_id;
}
return $item_ids;
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()
->id(),
];
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this
->getTranslationLanguages()) as $langcode) {
$translation = $this
->getTranslation($langcode);
if (!$translation
->getOwner()) {
$translation
->setOwnerId(0);
}
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
foreach ($this->bundle_items as $item) {
$item = $item->entity;
if ($item->bundle_id
->isEmpty()) {
$item->bundle_id = $this
->id();
$item
->save();
}
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
$bundleItems = [];
foreach ($entities as $entity) {
if (empty($entity->bundle_items)) {
continue;
}
foreach ($entity->bundle_items as $item) {
$bundleItems[$item->target_id] = $item->entity;
}
}
$storage = \Drupal::service('entity_type.manager')
->getStorage('commerce_product_bundle_i');
$storage
->delete($bundleItems);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setDescription(t('The user ID of author of the product bundle entity.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The title of the product bundle entity.'))
->setSettings([
'max_length' => 128,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['bundle_price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Bundle price'))
->setDescription(t('The product bundle base price. If set, the prices of the product bundle items will be ignored. Set only, if you want a global price per product bundle, independent from its items.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'commerce_price_default',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'commerce_price_default',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the product bundle is published.'))
->setDefaultValue(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
protected function ensureTranslations(array $entities) {
$langcode = $this
->language()
->getId();
foreach ($entities as $index => $entity) {
if ($entity
->hasTranslation($langcode)) {
$entities[$index] = $entity
->getTranslation($langcode);
}
}
return $entities;
}
}