WishlistPurchaseItem.php in Commerce Wishlist 8.3
File
src/Plugin/Field/FieldType/WishlistPurchaseItem.php
View source
<?php
namespace Drupal\commerce_wishlist\Plugin\Field\FieldType;
use Drupal\commerce_wishlist\WishlistPurchase;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataReferenceTargetDefinition;
class WishlistPurchaseItem extends FieldItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $storage) {
$properties = [];
$properties['order_id'] = DataReferenceTargetDefinition::create('integer')
->setLabel(t('Order ID'))
->setSetting('unsigned', TRUE);
$properties['quantity'] = DataDefinition::create('string')
->setLabel(t('Quantity'))
->setRequired(TRUE);
$properties['purchased'] = DataDefinition::create('timestamp')
->setLabel(t('Purchase time'))
->setRequired(TRUE);
return $properties;
}
public static function mainPropertyName() {
return NULL;
}
public static function schema(FieldStorageDefinitionInterface $storage) {
$columns = [];
$columns['order_id'] = [
'type' => 'int',
'default' => 0,
'unsigned' => TRUE,
];
$columns['quantity'] = [
'type' => 'numeric',
'default' => 1,
'unsigned' => TRUE,
];
$columns['purchased'] = [
'type' => 'int',
'default' => 0,
'unsigned' => TRUE,
];
return [
'columns' => $columns,
];
}
public function isEmpty() {
return empty($this->order_id) || empty($this->quantity) || empty($this->purchased);
}
public function setValue($values, $notify = TRUE) {
if ($values instanceof WishlistPurchase) {
$purchase = $values;
$values = [
'order_id' => $purchase
->getOrderId(),
'quantity' => $purchase
->getQuantity(),
'purchased' => $purchase
->getPurchasedTime(),
];
}
parent::setValue($values, $notify);
}
public function toPurchase() {
return new WishlistPurchase($this->order_id, $this->quantity, $this->purchased);
}
}