UcWishlist.php in UC Wish List 8
File
src/Entity/UcWishlist.php
View source
<?php
namespace Drupal\uc_wishlist\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;
class UcWishlist extends ContentEntityBase implements UcWishlistInterface {
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->getEntityKey('uid');
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['wid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Wishlist ID'))
->setDescription(t('The Wishlist ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The user UUID.'))
->setReadOnly(TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDefaultValue('');
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The username of the content author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\uc_wishlist\\Entity\\UcWishlist::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE);
$fields['expiration'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expiration'))
->setDescription(t('Timestamp for when the wish list expires.'));
$fields['address'] = BaseFieldDefinition::create('string')
->setLabel(t('Address'))
->setDescription(t('Address for shipping items on the wish list.'));
$fields['private'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Private'))
->setDescription(t('Whether the wishlist is private or not.'))
->setDefaultValue(FALSE);
$fields['description'] = BaseFieldDefinition::create('string')
->setLabel(t('Description'))
->setDescription(t('The description of the wish list.'));
return $fields;
}
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getExpirationTime() {
return $this
->get('expiration')->value;
}
public function setExpirationTime($expiration) {
$this
->set('expiration', $expiration);
return $this;
}
public function getAddress() {
return $this
->get('address')->value;
}
public function setAddress($address) {
$this
->set('address', $address);
return $this;
}
public function getPrivate() {
return $this
->get('private')->value;
}
public function setPrivate($private) {
$this
->set('private', $private);
return $this;
}
public function getDescription() {
return $this
->get('description')->value;
}
public function setDescription($description) {
$this
->set('description', $description);
return $this;
}
public function getUserId() {
return $this
->get('uid')->target_id;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
}