View source
<?php
namespace Drupal\commerce_store\Entity;
use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\FieldOverride;
use Drupal\address\AddressInterface;
use Drupal\commerce\EntityOwnerTrait;
use Drupal\commerce_price\Entity\CurrencyInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
class Store extends ContentEntityBase implements StoreInterface {
use EntityOwnerTrait;
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getEmail() {
return $this
->get('mail')->value;
}
public function setEmail($mail) {
$this
->set('mail', $mail);
return $this;
}
public function getDefaultCurrency() {
return $this
->get('default_currency')->entity;
}
public function setDefaultCurrency(CurrencyInterface $currency) {
$this
->set('default_currency', $currency
->getCurrencyCode());
return $this;
}
public function getDefaultCurrencyCode() {
return $this
->get('default_currency')->target_id;
}
public function setDefaultCurrencyCode($currency_code) {
$this
->set('default_currency', $currency_code);
return $this;
}
public function getTimezone() {
return $this
->get('timezone')->value;
}
public function setTimezone($timezone) {
$this
->set('timezone', $timezone);
return $this;
}
public function getAddress() {
return $this
->get('address')
->first();
}
public function setAddress(AddressInterface $address) {
$this->address = $address;
return $this;
}
public function getBillingCountries() {
$countries = [];
foreach ($this
->get('billing_countries') as $countryItem) {
$countries[] = $countryItem->value;
}
return $countries;
}
public function setBillingCountries(array $countries) {
$this
->set('billing_countries', $countries);
return $this;
}
public function isDefault() {
return (bool) $this
->get('is_default')->value;
}
public function setDefault($is_default) {
$this
->set('is_default', (bool) $is_default);
return $this;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this
->getTranslationLanguages()) as $langcode) {
$translation = $this
->getTranslation($langcode);
if ($translation
->getOwner()
->isAnonymous()) {
$translation
->setOwnerId(0);
}
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$default = $this
->isDefault();
$original_default = $this->original ? $this->original
->isDefault() : FALSE;
if ($default && !$original_default) {
$store_ids = $storage
->getQuery()
->condition('store_id', $this
->id(), '<>')
->condition('is_default', TRUE)
->accessCheck(FALSE)
->execute();
foreach ($store_ids as $store_id) {
$store = $storage
->load($store_id);
$store
->setDefault(FALSE);
$store
->save();
}
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The store type.'))
->setSetting('target_type', 'commerce_store_type')
->setReadOnly(TRUE);
$fields['uid']
->setLabel(t('Owner'))
->setDescription(t('The store owner.'))
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The store name.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setSettings([
'default_value' => '',
'max_length' => 255,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['mail'] = BaseFieldDefinition::create('email')
->setLabel(t('Email'))
->setDescription(t('Store email notifications are sent from this address.'))
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'email_default',
'weight' => 1,
])
->setSetting('display_description', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['default_currency'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Default currency'))
->setDescription(t('The default currency of the store.'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('target_type', 'commerce_currency')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 2,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['timezone'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Timezone'))
->setDescription(t('Used when determining promotion and tax availability.'))
->setCardinality(1)
->setRequired(TRUE)
->setDefaultValueCallback('Drupal\\commerce_store\\Entity\\Store::getSiteTimezone')
->setSetting('allowed_values_function', [
'\\Drupal\\commerce_store\\Entity\\Store',
'getTimezones',
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 3,
])
->setSetting('display_description', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['address'] = BaseFieldDefinition::create('address')
->setLabel(t('Address'))
->setDescription(t('The store address.'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('field_overrides', [
AddressField::GIVEN_NAME => [
'override' => FieldOverride::HIDDEN,
],
AddressField::ADDITIONAL_NAME => [
'override' => FieldOverride::HIDDEN,
],
AddressField::FAMILY_NAME => [
'override' => FieldOverride::HIDDEN,
],
AddressField::ORGANIZATION => [
'override' => FieldOverride::HIDDEN,
],
])
->setDisplayOptions('form', [
'type' => 'address_default',
'weight' => 4,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['billing_countries'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Supported billing countries'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('allowed_values_function', [
'\\Drupal\\commerce_store\\Entity\\Store',
'getAvailableCountries',
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 5,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['path'] = BaseFieldDefinition::create('path')
->setLabel(t('URL alias'))
->setDescription(t('The store URL alias.'))
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'path',
'weight' => 30,
])
->setDisplayConfigurable('form', TRUE)
->setCustomStorage(TRUE);
$fields['is_default'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Default'))
->setDescription(t('Whether this is the default store.'))
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 90,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
return $fields;
}
public static function getSiteTimezone() {
$site_timezone = \Drupal::config('system.date')
->get('timezone.default');
if (empty($site_timezone)) {
$site_timezone = @date_default_timezone_get();
}
return [
$site_timezone,
];
}
public static function getTimezones() {
return system_time_zones(NULL, TRUE);
}
public static function getAvailableCountries() {
return \Drupal::service('address.country_repository')
->getList();
}
}