Commit 098e9275 authored by bojanz's avatar bojanz

Create a storage handler for the Store entity, move the default store logic there.

parent 633754be
......@@ -10,6 +10,6 @@ services:
commerce_store.default_store_resolver:
class: Drupal\commerce_store\Resolver\DefaultStoreResolver
arguments: ['@config.factory', '@entity.manager']
arguments: ['@entity.manager']
tags:
- { name: commerce_store.store_resolver, priority: -100 }
......@@ -22,6 +22,7 @@ use Drupal\user\UserInterface;
* label = @Translation("Store"),
* bundle_label = @Translation("Store type"),
* handlers = {
* "storage" = "Drupal\commerce_store\StoreStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\commerce_store\StoreListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
......
......@@ -15,6 +15,25 @@ use Drupal\Core\Form\FormStateInterface;
*/
class StoreForm extends ContentEntityForm {
/**
* The store storage.
*
* @var \Drupal\commerce_store\StoreStorageInterface
*/
protected $storage;
/**
* Constructs a new StoreForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entityManager) {
parent::__construct($entityManager);
$this->storage = $this->entityManager->getStorage('commerce_store');
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::form().
*/
......@@ -23,22 +42,16 @@ class StoreForm extends ContentEntityForm {
$form = parent::form($form, $form_state);
$entity = $this->entity;
// Add default store config setting.
$default_store = \Drupal::config('commerce_store.settings')->get('default_store');
$is_default_store = FALSE;
$disabled = FALSE;
if (!empty($default_store) && $default_store == $entity->uuid()) {
$is_default_store = TRUE;
}
if (empty($default_store)) {
$is_default_store = TRUE;
$disabled = TRUE;
$defaultStore = $this->storage->loadDefault();
$isDefault = TRUE;
if ($defaultStore && $defaultStore->uuid() != $entity->uuid()) {
$isDefault = FALSE;
}
$form['is_default_store'] = [
$form['default'] = [
'#type' => 'checkbox',
'#title' => t('Default store'),
'#default_value' => $is_default_store,
'#disabled' => $disabled,
'#title' => $this->t('Default'),
'#default_value' => $isDefault,
'#disabled' => $isDefault || empty($defaultStore),
'#weight' => 0,
];
......@@ -51,9 +64,8 @@ class StoreForm extends ContentEntityForm {
public function save(array $form, FormStateInterface $form_state) {
try {
$this->entity->save();
// Save the default store config setting.
if (!$form_state->isValueEmpty('is_default_store') && $form_state->getValue('is_default_store') != FALSE) {
\Drupal::configFactory()->getEditable('commerce_store.settings')->set('default_store', $this->entity->uuid())->save();
if ($form_state->getValue('default')) {
$this->storage->markAsDefault($this->entity);
}
drupal_set_message($this->t('Saved the %label store.', [
'%label' => $this->entity->label(),
......
......@@ -11,48 +11,32 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityManagerInterface;
/**
* Returns the default store, if set.
* Returns the default store, if known.
*/
class DefaultStoreResolver implements StoreResolverInterface {
/**
* The configuration factory.
* The store storage.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
* @var \Drupal\commerce_store\StoreStorageInterface
*/
protected $configFactory;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
protected $storage;
/**
* Constructs a new DefaultStoreResolver object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
*/
public function __construct(ConfigFactoryInterface $configFactory, EntityManagerInterface $entityManager) {
$this->configFactory = $configFactory;
$this->entityManager = $entityManager;
public function __construct(EntityManagerInterface $entityManager) {
$this->storage = $entityManager->getStorage('commerce_store');
}
/**
* {@inheritdoc}
*/
public function resolve() {
$store = NULL;
$uuid = $this->configFactory->get('commerce_store.settings')->get('default_store');
if ($uuid) {
$store = $this->entityManager->loadEntityByUuid('commerce_store', $uuid);
}
return $store;
return $this->storage->loadDefault();
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_store\StoreStorage.
*/
namespace Drupal\commerce_store;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the store storage.
*/
class StoreStorage extends SqlContentEntityStorage implements StoreStorageInterface {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new StoreStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entityType
* The entity type definition.
* @param \Drupal\Core\Database\Connection $database
* The database connection to be used.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
*/
public function __construct(EntityTypeInterface $entityType, Connection $database, EntityManagerInterface $entityManager, CacheBackendInterface $cache, LanguageManagerInterface $languageManager, ConfigFactoryInterface $configFactory) {
parent::__construct($entityType, $database, $entityManager, $cache, $languageManager);
$this->configFactory = $configFactory;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entityType) {
return new static(
$entityType,
$container->get('database'),
$container->get('entity.manager'),
$container->get('cache.entity'),
$container->get('language_manager'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function loadDefault() {
$default = NULL;
$uuid = $this->configFactory->get('commerce_store.settings')->get('default_store');
if ($uuid) {
$entities = $this->loadByProperties(['uuid' => $uuid]);
$default = reset($entities);
}
return $default;
}
/**
* {@inheritdoc}
*/
public function markAsDefault(StoreInterface $store) {
$config = $this->configFactory->getEditable('commerce_store.settings');
if ($config->get('default_store') != $store->uuid()) {
$config->set('default_store', $store->uuid());
$config->save();
}
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_store\StoreStorageInterface.
*/
namespace Drupal\commerce_store;
/**
* Defines the interface for store storage.
*/
interface StoreStorageInterface {
/**
* Loads the default store.
*
* @return \Drupal\commerce_store\StoreInterface|null
* The default store, if known.
*/
public function loadDefault();
/**
* Marks the provided store as the default.
*
* @param \Drupal\commerce_store\StoreInterface $store
* The new default store.
*/
public function markAsDefault(StoreInterface $store);
}
......@@ -27,29 +27,22 @@ class DefaultStoreResolverTest extends UnitTestCase {
* {@inheritdoc}
*/
public function setUp() {
$config = $this->getMockBuilder('Drupal\Core\Config\Config')
$storage = $this->getMockBuilder('Drupal\commerce_store\StoreStorage')
->disableOriginalConstructor()
->getMock();
$config->expects($this->once())
->method('get')
->with('default_store')
->will($this->returnValue('fakeuuid'));
$configFactory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
$configFactory->expects($this->once())
->method('get')
->with('commerce_store.settings')
->will($this->returnValue($config));
$storage->expects($this->once())
->method('loadDefault')
->willReturn('testStore');
$entityManager = $this->getMockBuilder('\Drupal\Core\Entity\EntityManager')
->disableOriginalConstructor()
->getMock();
$entityManager->expects($this->once())
->method('loadEntityByUuid')
->with('commerce_store', 'fakeuuid')
->will($this->returnValue('testStore'));
->method('getStorage')
->with('commerce_store')
->willReturn($storage);
$this->resolver = new DefaultStoreResolver($configFactory, $entityManager);
$this->resolver = new DefaultStoreResolver($entityManager);
}
/**
......
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