You are here

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;

/**
 * Defines the user entity class.
 *
 * The base table name here is plural, despite Drupal table naming standards,
 * because "user" is a reserved word in many databases.
 *
 * @ContentEntityType(
 *   id = "uc_wishlist",
 *   label = @Translation("Wishlist"),
 *   handlers = {
 *     "list_builder" = "Drupal\uc_wishlist\UcWishlistListBuilder",
 *   },
 *   admin_permission = "administer wish lists",
 *   base_table = "uc_wishlists",
 *   entity_keys = {
 *     "id" = "wid",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "canonical" = "/wishlist/{wid}",
 *     "edit-form" = "/admin/store/customers/{wid}/edit",
 *     "cancel-form" = "/admin/store/customers/{wid}/cancel",
 *     "collection" = "/admin/store/customers/wishlists",
 *   },
 * )
 */
class UcWishlist extends ContentEntityBase implements UcWishlistInterface {

  /**
   * {@inheritdoc}
   */
  public function getOwner() {
    return $this
      ->get('uid')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwnerId() {
    return $this
      ->getEntityKey('uid');
  }

  /**
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $this
      ->set('uid', $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this
      ->set('uid', $account
      ->id());
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this
      ->get('title')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setTitle($title) {
    $this
      ->set('title', $title);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getExpirationTime() {
    return $this
      ->get('expiration')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setExpirationTime($expiration) {
    $this
      ->set('expiration', $expiration);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getAddress() {
    return $this
      ->get('address')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setAddress($address) {
    $this
      ->set('address', $address);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getPrivate() {
    return $this
      ->get('private')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setPrivate($private) {
    $this
      ->set('private', $private);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this
      ->get('description')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setDescription($description) {
    $this
      ->set('description', $description);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getUserId() {
    return $this
      ->get('uid')->target_id;
  }

  /**
   * Default value callback for 'uid' base field definition.
   *
   * @see ::baseFieldDefinitions()
   *
   * @return array
   *   An array of default values.
   */
  public static function getCurrentUserId() {
    return [
      \Drupal::currentUser()
        ->id(),
    ];
  }

}

Classes

Namesort descending Description
UcWishlist Defines the user entity class.