Commit 070d8d1d authored by bojanz's avatar bojanz

Issue #2464805: Complete the store data model

Added the address and countries fields to the store entity.
parent e380edf1
......@@ -4,6 +4,7 @@ description: 'Defines the Store entity and associated features.'
package: Commerce
core: 8.x
dependencies:
- address
- commerce
- commerce_price
- entity_reference
......
......@@ -7,6 +7,8 @@
namespace Drupal\commerce_store\Entity;
use CommerceGuys\Addressing\Enum\AddressField;
use Drupal\address\AddressInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
......@@ -151,6 +153,42 @@ class Store extends ContentEntityBase implements StoreInterface {
return $this;
}
/**
* {@inheritdoc}
*/
public function getAddress() {
return $this->get('address')->first();
}
/**
* {@inheritdoc}
*/
public function setAddress(AddressInterface $address) {
// $this->set('address', $address) results in the address being appended
// to the item list, instead of replacing the existing first item.
$this->address = $address;
return $this;
}
/**
* {@inheritdoc}
*/
public function getCountries() {
$countries = [];
foreach ($this->get('countries') as $countryItem) {
$countries[] = $countryItem->value;
}
return $countries;
}
/**
* {@inheritdoc}
*/
public function setCountries(array $countries) {
$this->set('countries', $countries);
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -232,6 +270,33 @@ class Store extends ContentEntityBase implements StoreInterface {
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
// Disable the recipient and organization fields on the store address.
$disabledFields = [AddressField::RECIPIENT, AddressField::ORGANIZATION];
$fields['address'] = BaseFieldDefinition::create('address')
->setLabel(t('Address'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('fields', array_diff(AddressField::getAll(), $disabledFields))
->setDisplayOptions('form', [
'type' => 'address_default',
'settings' => [],
'weight' => 3,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['countries'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Countries'))
->setDescription(t('The countries this store sells to.'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('allowed_values_function', 'commerce_store_available_countries')
->setDisplayOptions('form', array(
'type' => 'options_select',
'weight' => 4,
))
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
return $fields;
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\commerce_store;
use Drupal\address\AddressInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\EntityOwnerInterface;
......@@ -72,4 +73,44 @@ interface StoreInterface extends EntityInterface, EntityOwnerInterface {
*/
public function setDefaultCurrency($currencyCode);
/**
* Gets the store address.
*
* @return \Drupal\address\AddressInterface
* The store address.
*/
public function getAddress();
/**
* Sets the store address.
*
* @param \Drupal\address\AddressInterface $address
* The store address.
*
* @return \Drupal\commerce_store\StoreInterface
* The class instance that this method is called on.
*/
public function setAddress(AddressInterface $address);
/**
* Gets the store countries.
*
* If empty, it's assumed that the store sells to all countries.
*
* @return array
* A list of country codes.
*/
public function getCountries();
/**
* Sets the store countries.
*
* @param array $countries
* A list of country codes.
*
* @return \Drupal\commerce_store\StoreInterface
* The class instance that this method is called on.
*/
public function setCountries(array $countries);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment