View source
<?php
namespace Drupal\commerce_pricelist\Entity;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\commerce\Entity\CommerceContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
class PriceList extends CommerceContentEntityBase implements PriceListInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
public function getType() {
return $this
->bundle();
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
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('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function getStartDate() {
return new DrupalDateTime($this
->get('start_date')->value);
}
public function setStartDate(DrupalDateTime $start_date) {
$this
->get('start_date')->value = $start_date
->format('Y-m-d');
return $this;
}
public function getEndDate() {
if (!$this
->get('end_date')
->isEmpty()) {
return new DrupalDateTime($this
->get('end_date')->value);
}
}
public function setEndDate(DrupalDateTime $end_date = NULL) {
$this
->get('end_date')->value = $end_date ? $end_date
->format('Y-m-d') : NULL;
return $this;
}
public function getItemsIds() {
$price_list_item_ids = [];
foreach ($this
->get('field_price_list_item') as $field_item) {
$price_list_item_ids[] = $field_item->target_id;
}
return $price_list_item_ids;
}
public function getItems() {
return $this
->getTranslatedReferencedEntities('field_price_list_item');
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
foreach ($this->field_price_list_item as $item) {
$price_list_item = $item->entity;
if ($price_list_item->price_list_id
->isEmpty()) {
$price_list_item->price_list_id = $this
->id();
$price_list_item
->save();
}
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
$price_list_items = [];
foreach ($entities as $entity) {
if (empty($entity->field_price_list_item)) {
continue;
}
foreach ($entity->field_price_list_item as $item) {
$price_list_items[$item->target_id] = $item->entity;
}
$price_list_item_storage = \Drupal::service('entity_type.manager')
->getStorage('price_list_item');
$price_list_item_storage
->delete($price_list_items);
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('The weight of this pricelist in relation to other pricelists.'))
->setDefaultValue(0);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Price list is published.'))
->setDefaultValue(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Price list entity.'))
->setRequired(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 1,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Price list entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\node\\Entity\\Node::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 2,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 2,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['start_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('Start date'))
->setDescription(t('The start date of the Price list entity.'))
->setRevisionable(TRUE)
->setSettings([
'datetime_type' => 'datetime',
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'datetime_default',
'settings' => [
'format_type' => 'medium',
],
'weight' => 3,
])
->setDisplayOptions('form', [
'type' => 'datetime_default',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['end_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('End date'))
->setDescription(t('The end date of the Price list entity.'))
->setRevisionable(TRUE)
->setSettings([
'datetime_type' => 'datetime',
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'datetime_default',
'settings' => [
'format_type' => 'medium',
],
'weight' => 4,
])
->setDisplayOptions('form', [
'type' => 'datetime_default',
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', 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;
}
}