View source
<?php
namespace Drupal\commerce_wishlist\Entity;
use Drupal\Component\Utility\Random;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Url;
use Drupal\profile\Entity\ProfileInterface;
use Drupal\user\EntityOwnerTrait;
class Wishlist extends ContentEntityBase implements WishlistInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
public function createDuplicate() {
$duplicate = parent::createDuplicate();
$duplicate
->set('code', NULL);
$duplicate
->set('wishlist_items', []);
return $duplicate;
}
public function toUrl($rel = 'canonical', array $options = []) {
if (in_array($rel, [
'canonical',
'revision',
])) {
$route_name = 'entity.commerce_wishlist.canonical';
$route_parameters = [
'code' => $this
->getCode(),
];
$options += [
'entity_type' => 'commerce_wishlist',
'entity' => $this,
'language' => $this
->language(),
];
return new Url($route_name, $route_parameters, $options);
}
else {
return parent::toUrl($rel, $options);
}
}
protected function urlRouteParameters($rel) {
if (in_array($rel, [
'user-form',
'share-form',
])) {
return [
'user' => $this
->getOwnerId(),
'code' => $this
->getCode(),
];
}
else {
return parent::urlRouteParameters($rel);
}
}
public function getCode() {
return $this
->get('code')->value;
}
public function setCode($code) {
$this
->set('code', $code);
return $this;
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getShippingProfile() {
return $this
->get('shipping_profile')->entity;
}
public function setShippingProfile(ProfileInterface $profile) {
$this
->set('shipping_profile', $profile);
return $this;
}
public function getItems() {
return $this
->get('wishlist_items')
->referencedEntities();
}
public function setItems(array $wishlist_items) {
$this
->set('wishlist_items', $wishlist_items);
return $this;
}
public function hasItems() {
return !$this
->get('wishlist_items')
->isEmpty();
}
public function addItem(WishlistItemInterface $wishlist_item) {
if (!$this
->hasItem($wishlist_item)) {
$this
->get('wishlist_items')
->appendItem($wishlist_item);
}
return $this;
}
public function removeItem(WishlistItemInterface $wishlist_item) {
$index = $this
->getItemIndex($wishlist_item);
if ($index !== FALSE) {
$this
->get('wishlist_items')
->offsetUnset($index);
}
return $this;
}
public function hasItem(WishlistItemInterface $wishlist_item) {
return $this
->getItemIndex($wishlist_item) !== FALSE;
}
protected function getItemIndex(WishlistItemInterface $wishlist_item) {
$values = $this
->get('wishlist_items')
->getValue();
$wishlist_item_ids = array_map(function ($value) {
return $value['target_id'];
}, $values);
return array_search($wishlist_item
->id(), $wishlist_item_ids);
}
public function isDefault() {
return (bool) $this
->get('is_default')->value;
}
public function setDefault($default) {
$this
->set('is_default', (bool) $default);
return $this;
}
public function isPublic() {
return (bool) $this
->get('is_public')->value;
}
public function setPublic($public) {
$this
->set('is_public', (bool) $public);
return $this;
}
public function getKeepPurchasedItems() {
return (bool) $this
->get('keep_purchased_items')->value;
}
public function setKeepPurchasedItems($keep_purchased_items) {
$this
->set('keep_purchased_items', (bool) $keep_purchased_items);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if ($this
->get('code')
->isEmpty()) {
$storage = $this
->entityTypeManager()
->getStorage('commerce_wishlist');
$random = new Random();
$code = $random
->word(13);
while ($storage
->loadByCode($code)) {
$code = $random
->word(13);
}
$this
->setCode($random
->word(13));
}
if ($this
->getOwnerId() && !$this
->isDefault()) {
$wishlist = $storage
->loadDefaultByUser($this
->getOwner(), $this
->bundle());
if (!$wishlist || !$wishlist
->isDefault()) {
$this
->setDefault(TRUE);
}
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
foreach ($this
->getItems() as $wishlist_item) {
if ($wishlist_item->wishlist_id
->isEmpty()) {
$wishlist_item->wishlist_id = $this
->id();
$wishlist_item
->save();
}
}
if ($this
->getOwnerId()) {
$default = $this
->isDefault();
$original_default = $this->original ? $this->original
->isDefault() : FALSE;
if ($default && !$original_default) {
$wishlists = $storage
->loadMultipleByUser($this
->getOwner(), $this
->bundle());
foreach ($wishlists as $wishlist) {
if ($wishlist
->id() != $this
->id() && $wishlist
->isDefault()) {
$wishlist
->setDefault(FALSE);
$wishlist
->save();
}
}
}
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
$wishlist_items = [];
foreach ($entities as $entity) {
foreach ($entity
->getItems() as $wishlist_item) {
$wishlist_items[$wishlist_item
->id()] = $wishlist_item;
}
}
$wishlist_item_storage = \Drupal::service('entity_type.manager')
->getStorage('commerce_wishlist_item');
$wishlist_item_storage
->delete($wishlist_items);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['code'] = BaseFieldDefinition::create('string')
->setLabel(t('Code'))
->setDescription(t('The wishlist code.'))
->setSetting('max_length', 25)
->addConstraint('UniqueField', []);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The wishlist name.'))
->setRequired(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['uid']
->setLabel(t('Owner'))
->setDescription(t('The wishlist owner.'))
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['shipping_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
->setLabel(t('Shipping profile'))
->setDescription(t('Shipping profile'))
->setSetting('target_type', 'profile')
->setSetting('handler', 'default')
->setSetting('handler_settings', [
'target_bundles' => [
'customer',
],
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 0,
'settings' => [],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['wishlist_items'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Wishlist items'))
->setDescription(t('The wishlist items.'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'commerce_wishlist_item')
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'type' => 'commerce_wishlist_item_table',
'weight' => 0,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', TRUE);
$fields['is_default'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Default'))
->setDescription(t('A boolean indicating whether the wishlist is the default one.'));
$fields['is_public'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Public'))
->setDescription(t('Whether the wishlist is public.'))
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 19,
])
->setDisplayConfigurable('form', TRUE);
$fields['keep_purchased_items'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Keep purchased items in the list'))
->setDescription(t('Whether items should remain in the wishlist once purchased.'))
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the wishlist was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the wishlist was last edited.'));
return $fields;
}
}