View source
<?php
namespace Drupal\commerce_pricelist\Entity;
use Drupal\commerce_price\Price;
use Drupal\commerce\Entity\CommerceContentEntityBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
class PriceListItem extends CommerceContentEntityBase implements PriceListItemInterface {
use EntityChangedTrait;
public function getPriceList() {
return $this
->get('price_list_id')->entity;
}
public function setPriceList(PriceListInterface $price_list) {
return $this
->set('price_list_id', $price_list);
}
public function getPriceListId() {
return $this
->get('price_list_id')->target_id;
}
public function setPriceListId($price_list_id) {
return $this
->set('price_list_id', $price_list_id);
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getQuantity() {
return $this
->get('quantity')->value;
}
public function setQuantity($quantity) {
$this
->set('quantity', $quantity);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getWeight() {
return $this
->get('weight')->value;
}
public function setWeight($weight) {
$this
->set('weight', $weight);
return $this;
}
public function setPrice(Price $price) {
return $this
->set('price', $price);
}
public function getPrice() {
if (!$this
->get('price')
->isEmpty()) {
return $this
->get('price')
->first()
->toPrice();
}
}
public function hasPurchasedEntity() {
return !$this
->get('purchased_entity')
->isEmpty();
}
public function getPurchasedEntity() {
return $this
->getTranslatedReferencedEntity('purchased_entity');
}
public function getPurchasedEntityId() {
return $this
->get('purchased_entity')->target_id;
}
public function setPurchasedEntityId($purchased_entity_id) {
return $this
->set('purchased_entity', $purchased_entity_id);
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function isActive() {
return (bool) $this
->getEntityKey('status');
}
public function setActive() {
$this
->set('status', (bool) TRUE);
return $this;
}
public function setInactive() {
$this
->set('status', (bool) FALSE);
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setDescription(t('The price list item author.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\commerce_pricelist\\Entity\\PriceListItem::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('The Weight of the Price list item entity.'))
->setDefaultValue(0);
$fields['price_list_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Price list'))
->setDescription(t('The parent price list of the Price list item entity.'))
->setSetting('target_type', 'price_list')
->setRequired(TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['purchased_entity'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Purchased entity'))
->setDescription(t('The purchased entity of the price list item.'))
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -1,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('Optional label for this price list item.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 2,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 2,
])
->setTranslatable(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['quantity'] = BaseFieldDefinition::create('integer')
->setLabel(t('Quantity'))
->setDescription(t('The product quantity number of the Price list item entity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'integer',
'weight' => 3,
])
->setDisplayOptions('form', [
'type' => 'integer',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Price'))
->setDescription(t('The price of the Price list item entity.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'commerce_price_default',
'weight' => 4,
])
->setDisplayOptions('form', [
'type' => 'commerce_price_default',
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Active'))
->setDescription(t('Whether the price list item is active.'))
->setDefaultValue(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 99,
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the price list item was created.'))
->setTranslatable(TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'))
->setTranslatable(TRUE);
return $fields;
}
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
$price_list_item_type = PriceListItemType::load($bundle);
$purchasable_entity_type_id = $price_list_item_type
->getPurchasableEntityTypeId();
$purchasable_entity_type_label = $price_list_item_type
->getPurchasableEntityType()
->getLabel();
$fields = [];
$fields['purchased_entity'] = clone $base_field_definitions['purchased_entity'];
if ($purchasable_entity_type_id) {
$fields['purchased_entity']
->setSetting('target_type', $purchasable_entity_type_id);
$fields['purchased_entity']
->setLabel($purchasable_entity_type_label);
}
else {
$fields['purchased_entity']
->setRequired(FALSE);
$fields['purchased_entity']
->setDisplayOptions('form', [
'type' => 'hidden',
]);
$fields['purchased_entity']
->setDisplayConfigurable('form', FALSE);
$fields['purchased_entity']
->setDisplayConfigurable('view', FALSE);
$fields['purchased_entity']
->setReadOnly(TRUE);
}
return $fields;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
}