Commit c106f094 authored by bojanz's avatar bojanz

Start following the Drupal coding standard for local variables (snakeCase ->...

Start following the Drupal coding standard for local variables (snakeCase -> camelCase) in commerce and commerce_store.
parent 39f4a9c3
......@@ -8,11 +8,9 @@
namespace Drupal\commerce_store\Command;
use CommerceGuys\Intl\Currency\CurrencyRepository;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Drupal\Console\Command\ContainerAwareCommand;
......@@ -36,38 +34,38 @@ class CreateStoreCommand extends ContainerAwareCommand {
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$container = $this->getContainer();
$currencyImporter = $container->get('commerce_price.currency_importer');
$storeStorage = $container->get('entity_type.manager')->getStorage('commerce_store');
$currency_importer = $container->get('commerce_price.currency_importer');
$store_storage = $container->get('entity_type.manager')->getStorage('commerce_store');
$countryCode = $input->getOption('country');
$currencyCode = $input->getOption('currency');
$country_code = $input->getOption('country');
$currency_code = $input->getOption('currency');
// Allow the caller to specify the country, but not the currency.
if (empty($currencyCode)) {
$countryRepository = $container->get('address.country_repository');
$country = $countryRepository->get($countryCode, 'en');
$currencyCode = $country->getCurrencyCode();
if (empty($currencyCode)) {
$message = sprintf('Default currency not known for country %s, please specify one via --currency.', $countryCode);
if (empty($currency_code)) {
$country_repository = $container->get('address.country_repository');
$country = $country_repository->get($country_code, 'en');
$currency_code = $country->getCurrencyCode();
if (empty($currency_code)) {
$message = sprintf('Default currency not known for country %s, please specify one via --currency.', $country_code);
throw new \RuntimeException($message);
}
}
$currency = $currencyImporter->import($currencyCode);
$currency_importer->import($currency_code);
$values = [
'type' => 'default',
'uid' => 1,
'name' => $input->getOption('name'),
'mail' => $input->getOption('mail'),
'address' => [
'country_code' => $countryCode,
'country_code' => $country_code,
],
'default_currency' => $currencyCode,
'default_currency' => $currency_code,
];
$store = $storeStorage->create($values);
$store = $store_storage->create($values);
$store->save();
// Make this the default store, since there's no other.
if (!$storeStorage->loadDefault()) {
$storeStorage->markAsDefault($store);
if (!$store_storage->loadDefault()) {
$store_storage->markAsDefault($store);
}
$link = $container->get('url_generator')->generate('entity.commerce_store.edit_form', ['commerce_store' => $store->id()], TRUE);
......@@ -79,12 +77,12 @@ class CreateStoreCommand extends ContainerAwareCommand {
*/
protected function interact(InputInterface $input, OutputInterface $output) {
$container = $this->getContainer();
$emailValidator = $container->get('email.validator');
$countryRepository = $container->get('address.country_repository');
$currencyRepository = new CurrencyRepository();
$mail_validator = $container->get('email.validator');
$country_repository = $container->get('address.country_repository');
$currency_repository = new CurrencyRepository();
$helper = $this->getHelper('question');
// Symfony Console has no built-in way to ensure the value is not empty.
$requiredValidator = function ($value) {
$required_validator = function ($value) {
if (empty($value)) {
throw new \RuntimeException("Value can't be empty.");
}
......@@ -95,7 +93,7 @@ class CreateStoreCommand extends ContainerAwareCommand {
$name = $input->getOption('name');
if (!$name) {
$question = new Question('Enter the store name: ', '');
$question->setValidator($requiredValidator);
$question->setValidator($required_validator);
$name = $helper->ask($input, $output, $question);
}
$input->setOption('name', $name);
......@@ -103,8 +101,8 @@ class CreateStoreCommand extends ContainerAwareCommand {
$mail = $input->getOption('mail');
if (!$mail) {
$question = new Question('Enter the store email: ', '');
$question->setValidator(function ($mail) use ($emailValidator) {
if (empty($mail) || !$emailValidator->isValid($mail)) {
$question->setValidator(function ($mail) use ($mail_validator) {
if (empty($mail) || !$mail_validator->isValid($mail)) {
throw new \RuntimeException('The entered email is not valid.');
}
return $mail;
......@@ -115,27 +113,27 @@ class CreateStoreCommand extends ContainerAwareCommand {
// --country option.
$country = $input->getOption('country');
if (!$country) {
$countryNames = array_flip($countryRepository->getList('en'));
$country_names = array_flip($country_repository->getList('en'));
$question = new Question('Enter the store country: ', '');
$question->setAutocompleterValues($countryNames);
$question->setValidator($requiredValidator);
$question->setAutocompleterValues($country_names);
$question->setValidator($required_validator);
$country = $helper->ask($input, $output, $question);
$country = $countryNames[$country];
$country = $country_names[$country];
}
$input->setOption('country', $country);
// --currency option.
$currency = $input->getOption('currency');
if (!$currency) {
$country = $countryRepository->get($country, 'en');
$currencyCode = $country->getCurrencyCode();
if ($currencyCode) {
$question = new Question("Enter the store currency [$currencyCode]: ", $currencyCode);
$country = $country_repository->get($country, 'en');
$currency_code = $country->getCurrencyCode();
if ($currency_code) {
$question = new Question("Enter the store currency [$currency_code]: ", $currency_code);
}
else {
$question = new Question('Enter the store currency: ');
}
$question->setAutocompleterValues(array_keys($currencyRepository->getList('en')));
$question->setValidator($requiredValidator);
$question->setAutocompleterValues(array_keys($currency_repository->getList('en')));
$question->setValidator($required_validator);
$currency = $helper->ask($input, $output, $question);
}
$input->setOption('currency', $currency);
......
......@@ -23,7 +23,7 @@ interface EntityStoreInterface {
/**
* Sets the store.
*
* @return \Drupal\commerce_store\Entity\StoreInterface $store
* @param \Drupal\commerce_store\Entity\StoreInterface $store
* The store entity.
*
* @return $this
......
......@@ -214,8 +214,8 @@ class Store extends ContentEntityBase implements StoreInterface {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entityType) {
$fields = self::entityKeysBaseFieldDefinitions($entityType);
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = self::entityKeysBaseFieldDefinitions($entity_type);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
......@@ -277,13 +277,13 @@ class Store extends ContentEntityBase implements StoreInterface {
->setDisplayConfigurable('form', TRUE);
// Disable the recipient and organization fields on the store address.
$disabledFields = [AddressField::RECIPIENT, AddressField::ORGANIZATION];
$disabled_fields = [AddressField::RECIPIENT, AddressField::ORGANIZATION];
$fields['address'] = BaseFieldDefinition::create('address')
->setLabel(t('Address'))
->setDescription(t('The store address.'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('fields', array_diff(AddressField::getAll(), $disabledFields))
->setSetting('fields', array_diff(AddressField::getAll(), $disabled_fields))
->setDisplayOptions('form', [
'type' => 'address_default',
'settings' => [],
......
......@@ -43,16 +43,16 @@ class StoreForm extends ContentEntityForm {
$form = parent::form($form, $form_state);
$entity = $this->entity;
$defaultStore = $this->storage->loadDefault();
$default_store = $this->storage->loadDefault();
$isDefault = TRUE;
if ($defaultStore && $defaultStore->uuid() != $entity->uuid()) {
if ($default_store && $default_store->uuid() != $entity->uuid()) {
$isDefault = FALSE;
}
$form['default'] = [
'#type' => 'checkbox',
'#title' => $this->t('Default'),
'#default_value' => $isDefault,
'#disabled' => $isDefault || empty($defaultStore),
'#disabled' => $isDefault || empty($default_store),
'#weight' => 99,
];
......
......@@ -27,11 +27,11 @@ class StoreTypeDeleteForm extends EntityDeleteForm {
/**
* Constructs a new StoreTypeDeleteForm object.
*
* @param \Drupal\Core\Entity\Query\QueryFactory $queryFactory
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* The entity query object.
*/
public function __construct(QueryFactory $queryFactory) {
$this->queryFactory = $queryFactory;
public function __construct(QueryFactory $query_factory) {
$this->queryFactory = $query_factory;
}
/**
......@@ -47,12 +47,12 @@ class StoreTypeDeleteForm extends EntityDeleteForm {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$numStores = $this->queryFactory->get('commerce_store')
$store_count = $this->queryFactory->get('commerce_store')
->condition('type', $this->entity->id())
->count()
->execute();
if ($numStores) {
$caption = '<p>' . $this->formatPlural($numStores, '%type is used by 1 store on your site. You can not remove this store type until you have removed all of the %type stores.', '%type is used by @count stores on your site. You may not remove %type until you have removed all of the %type stores.', ['%type' => $this->entity->label()]) . '</p>';
if ($store_count) {
$caption = '<p>' . $this->formatPlural($store_count, '%type is used by 1 store on your site. You can not remove this store type until you have removed all of the %type stores.', '%type is used by @count stores on your site. You may not remove %type until you have removed all of the %type stores.', ['%type' => $this->entity->label()]) . '</p>';
$form['#title'] = $this->getQuestion();
$form['description'] = ['#markup' => $caption];
return $form;
......
......@@ -19,19 +19,19 @@ class StoreTypeForm extends BundleEntityFormBase {
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$storeType = $this->entity;
$store_type = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $storeType->label(),
'#default_value' => $store_type->label(),
'#description' => $this->t('Label for the store type.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $storeType->id(),
'#default_value' => $store_type->id(),
'#machine_name' => [
'exists' => '\Drupal\commerce_store\Entity\StoreType::load',
],
......@@ -40,7 +40,7 @@ class StoreTypeForm extends BundleEntityFormBase {
$form['description'] = [
'#type' => 'textfield',
'#title' => $this->t('Description'),
'#default_value' => $storeType->getDescription(),
'#default_value' => $store_type->getDescription(),
];
if ($this->moduleHandler->moduleExists('language')) {
......@@ -53,9 +53,9 @@ class StoreTypeForm extends BundleEntityFormBase {
'#type' => 'language_configuration',
'#entity_information' => [
'entity_type' => 'commerce_store',
'bundle' => $storeType->id(),
'bundle' => $store_type->id(),
],
'#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_store', $storeType->id()),
'#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_store', $store_type->id()),
];
$form['#submit'][] = 'language_configuration_element_submit';
}
......
......@@ -24,11 +24,11 @@ class DefaultStoreResolver implements StoreResolverInterface {
/**
* Constructs a new DefaultStoreResolver object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->storage = $entityTypeManager->getStorage('commerce_store');
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->storage = $entity_type_manager->getStorage('commerce_store');
}
/**
......
......@@ -50,14 +50,14 @@ class StoreContext implements StoreContextInterface {
/**
* Constructs a new StoreContext object.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\commerce_store\Resolver\ChainStoreResolverInterface $chainResolver
* @param \Drupal\commerce_store\Resolver\ChainStoreResolverInterface $chain_resolver
* The chain resolver.
*/
public function __construct(RequestStack $requestStack, ChainStoreResolverInterface $chainResolver) {
$this->requestStack = $requestStack;
$this->chainResolver = $chainResolver;
public function __construct(RequestStack $request_stack, ChainStoreResolverInterface $chain_resolver) {
$this->requestStack = $request_stack;
$this->chainResolver = $chain_resolver;
$this->stores = new \SplObjectStorage();
}
......
......@@ -50,8 +50,8 @@ class StoreTest extends StoreTestBase {
'default_currency' => 'EUR',
]
);
$storeExist = (bool) Store::load($store->id());
$this->assertTrue($storeExist, 'The new store has been created in the database.');
$store_exists = (bool) Store::load($store->id());
$this->assertTrue($store_exists, 'The new store has been created in the database.');
// Create a store through the form.
$this->drupalGet('admin/commerce/stores');
......@@ -84,8 +84,8 @@ class StoreTest extends StoreTestBase {
'name[0][value]' => $this->randomMachineName(8),
];
$this->drupalPostForm(NULL, $edit, 'Save');
$storeChanged = Store::load($store->id());
$this->assertEqual($store->getName(), $storeChanged->getName(), 'The name of the store has been changed.');
$store_changed = Store::load($store->id());
$this->assertEqual($store->getName(), $store_changed->getName(), 'The name of the store has been changed.');
}
/**
......@@ -99,12 +99,12 @@ class StoreTest extends StoreTestBase {
'email' => \Drupal::currentUser()->getEmail(),
]
);
$storeExist = (bool) Store::load($store->id());
$this->assertTrue($storeExist, 'The new store has been created in the database.');
$store_exists = (bool) Store::load($store->id());
$this->assertTrue($store_exists, 'The new store has been created in the database.');
// Delete the Store and verify deletion.
$store->delete();
$storeExist = (bool) Store::load($store->id());
$this->assertFalse($storeExist, 'The new store has been deleted from the database.');
$store_exists = (bool) Store::load($store->id());
$this->assertFalse($store_exists, 'The new store has been deleted from the database.');
}
}
......@@ -21,12 +21,12 @@ class StoreTypeTest extends StoreTestBase {
*/
public function testDefaultStoreType() {
$this->drupalGet('admin/commerce/config/store-types');
$storeTypes = StoreType::loadMultiple();
$store_types = StoreType::loadMultiple();
$this->assertTrue(isset($storeTypes['default']), 'The default store type is available');
$this->assertTrue(isset($store_types['default']), 'The default store type is available');
$storeType = StoreType::load('default');
$this->assertEqual($storeTypes['default'], $storeType, 'The correct store type is loaded');
$store_type = StoreType::load('default');
$this->assertEqual($store_types['default'], $store_type, 'The correct store type is loaded');
}
/**
......@@ -34,12 +34,12 @@ class StoreTypeTest extends StoreTestBase {
*/
public function testListStoreType() {
$title = strtolower($this->randomMachineName(8));
$tableSelector = 'table tbody tr';
$table_selector = 'table tbody tr';
// The store shows one default store type.
$this->drupalGet('admin/commerce/config/store-types');
$storeTypes = $this->cssSelect($tableSelector);
$this->assertEqual(count($storeTypes), 1, 'Stores types are correctly listed');
$store_types = $this->cssSelect($table_selector);
$this->assertEqual(count($store_types), 1, 'Stores types are correctly listed');
// Create a new commerce store type entity and see if the list has two store types.
$this->createEntity('commerce_store_type', [
......@@ -49,8 +49,8 @@ class StoreTypeTest extends StoreTestBase {
);
$this->drupalGet('admin/commerce/config/store-types');
$storeTypes = $this->cssSelect($tableSelector);
$this->assertEqual(count($storeTypes), 2, 'Stores types are correctly listed');
$store_types = $this->cssSelect($table_selector);
$this->assertEqual(count($store_types), 2, 'Stores types are correctly listed');
}
/**
......@@ -66,8 +66,8 @@ class StoreTypeTest extends StoreTestBase {
]
);
$typeExists = (bool) StoreType::load($type->id());
$this->assertTrue($typeExists, 'The new store type has been created in the database.');
$type_exists = (bool) StoreType::load($type->id());
$this->assertTrue($type_exists, 'The new store type has been created in the database.');
// Create a store type through the form.
$edit = [
......@@ -75,8 +75,8 @@ class StoreTypeTest extends StoreTestBase {
'label' => 'Label of foo',
];
$this->drupalPostForm('admin/commerce/config/store-types/add', $edit, t('Save'));
$typeExists = (bool) StoreType::load($edit['id']);
$this->assertTrue($typeExists, 'The new store type has been created in the database.');
$type_exists = (bool) StoreType::load($edit['id']);
$this->assertTrue($type_exists, 'The new store type has been created in the database.');
}
/**
......@@ -84,7 +84,7 @@ class StoreTypeTest extends StoreTestBase {
*/
public function testUpdateStoreType() {
// Create a new store type.
$storeType = $this->createEntity('commerce_store_type', [
$store_type = $this->createEntity('commerce_store_type', [
'id' => 'foo',
'label' => 'Label for foo',
]
......@@ -95,8 +95,8 @@ class StoreTypeTest extends StoreTestBase {
'label' => $this->randomMachineName(8),
];
$this->drupalPostForm('admin/commerce/config/store-types/default/edit', $edit, 'Save');
$storeTypeChanged = StoreType::load($storeType->id());
$this->assertEqual($storeType->label(), $storeTypeChanged->label(), 'The label of the store type has been changed.');
$changed = StoreType::load($store_type->id());
$this->assertEqual($store_type->label(), $changed->label(), 'The label of the store type has been changed.');
}
/**
......@@ -134,8 +134,8 @@ class StoreTypeTest extends StoreTestBase {
);
$this->assertText(t('This action cannot be undone.'), 'The store type deletion confirmation form is available');
$this->drupalPostForm(NULL, NULL, t('Delete'));
$typeExists = (bool) StoreType::load($type->id());
$this->assertFalse($typeExists, 'The new store type has been deleted from the database.');
$type_exists = (bool) StoreType::load($type->id());
$this->assertFalse($type_exists, 'The new store type has been deleted from the database.');
}
}
......@@ -37,28 +37,28 @@ class ChainStoreResolverTest extends UnitTestCase {
* ::covers resolve
*/
public function testResolver() {
$mockupBuilder = $this->getMockBuilder('Drupal\commerce_store\Resolver\StoreResolverInterface')
$mock_builder = $this->getMockBuilder('Drupal\commerce_store\Resolver\StoreResolverInterface')
->disableOriginalConstructor();
$firstResolver = $mockupBuilder->getMock();
$firstResolver->expects($this->once())
$first_resolver = $mock_builder->getMock();
$first_resolver->expects($this->once())
->method('resolve');
$secondResolver = $mockupBuilder->getMock();
$secondResolver->expects($this->once())
$second_resolver = $mock_builder->getMock();
$second_resolver->expects($this->once())
->method('resolve')
->willReturn('testStore');
$thirdResolver = $mockupBuilder->getMock();
$thirdResolver->expects($this->never())
$third_resolver = $mock_builder->getMock();
$third_resolver->expects($this->never())
->method('resolve');
$this->resolver->addResolver($firstResolver);
$this->resolver->addResolver($secondResolver);
$this->resolver->addResolver($thirdResolver);
$this->resolver->addResolver($first_resolver);
$this->resolver->addResolver($second_resolver);
$this->resolver->addResolver($third_resolver);
$resolvers = $this->resolver->getResolvers();
$this->assertEquals([$firstResolver, $secondResolver, $thirdResolver], $resolvers);
$this->assertEquals([$first_resolver, $second_resolver, $third_resolver], $resolvers);
$result = $this->resolver->resolve();
$this->assertEquals('testStore', $result);
......
......@@ -34,23 +34,22 @@ class DefaultStoreResolverTest extends UnitTestCase {
->method('loadDefault')
->willReturn('testStore');
$entityTypeManager = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeManager')
$entity_type_manager = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeManager')
->disableOriginalConstructor()
->getMock();
$entityTypeManager->expects($this->once())
$entity_type_manager->expects($this->once())
->method('getStorage')
->with('commerce_store')
->willReturn($storage);
$this->resolver = new DefaultStoreResolver($entityTypeManager);
$this->resolver = new DefaultStoreResolver($entity_type_manager);
}
/**
* @covers ::resolve
*/
public function testResolve() {
$defaultStore = $this->resolver->resolve();
$this->assertEquals('testStore', $defaultStore);
$this->assertEquals('testStore', $this->resolver->resolve());
}
}
......@@ -34,31 +34,31 @@ class CommerceContentEntityStorage extends SqlContentEntityStorage {
/**
* Constructs a new CommerceContentEntityStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entityType
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Database\Connection $database
* The database connection to be used.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
*/
public function __construct(EntityTypeInterface $entityType, Connection $database, EntityManagerInterface $entityManager, CacheBackendInterface $cache, LanguageManagerInterface $languageManager, EventDispatcherInterface $eventDispatcher) {
parent::__construct($entityType, $database, $entityManager, $cache, $languageManager);
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher) {
parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager);
$this->eventDispatcher = $eventDispatcher;
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entityType) {
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entityType,
$entity_type,
$container->get('database'),
$container->get('entity.manager'),
$container->get('cache.entity'),
......@@ -73,16 +73,16 @@ class CommerceContentEntityStorage extends SqlContentEntityStorage {
protected function postLoad(array &$entities) {
parent::postLoad($entities);
$eventClass = $this->entityType->getHandlerClass('event');
if (!$eventClass) {
$event_class = $this->entityType->getHandlerClass('event');
if (!$event_class) {
return;
}
// hook_entity_load() is invoked for all entities at once.
// The event is dispatched for each entity separately, for better DX.
// @todo Evaluate performance implications.
$eventName = $this->getEventName('load');
$event_name = $this->getEventName('load');
foreach ($entities as $entity) {
$this->eventDispatcher->dispatch($eventName, new $eventClass($entity));
$this->eventDispatcher->dispatch($event_name, new $event_class($entity));
}
}
......@@ -92,9 +92,9 @@ class CommerceContentEntityStorage extends SqlContentEntityStorage {
protected function invokeHook($hook, EntityInterface $entity) {
parent::invokeHook($hook, $entity);
$eventClass = $this->entityType->getHandlerClass('event');
if ($eventClass) {
$this->eventDispatcher->dispatch($this->getEventName($hook), new $eventClass($entity));
$event_class = $this->entityType->getHandlerClass('event');
if ($event_class) {
$this->eventDispatcher->dispatch($this->getEventName($hook), new $event_class($entity));
}
}
......
......@@ -22,11 +22,11 @@ final class Country {
/**
* Constructs a new Country object.
*
* @param string $countryCode
* @param string $country_code
* The country code.
*/
public function __construct($countryCode) {
$this->countryCode = strtoupper($countryCode);
public function __construct($country_code) {
$this->countryCode = strtoupper($country_code);
}
/**
......
......@@ -47,14 +47,14 @@ class CountryContext implements CountryContextInterface {
/**
* Constructs a new CountryContext object.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\commerce\Resolver\ChainCountryResolverInterface $chainResolver
* @param \Drupal\commerce\Resolver\ChainCountryResolverInterface $chain_resolver
* The chain resolver.
*/
public function __construct(RequestStack $requestStack, ChainCountryResolverInterface $chainResolver) {
$this->requestStack = $requestStack;
$this->chainResolver = $chainResolver;
public function __construct(RequestStack $request_stack, ChainCountryResolverInterface $chain_resolver) {
$this->requestStack = $request_stack;
$this->chainResolver = $chain_resolver;
$this->countries = new \SplObjectStorage();
}
......
......@@ -66,54 +66,54 @@ class EntitySelect extends FormElement {
/**
* Process callback.
*/
public static function processEntitySelect(&$element, FormStateInterface $formState, &$completeForm) {
public static function processEntitySelect(&$element, FormStateInterface $form_state, &$complete_form) {
// Nothing to do if there is no target entity type.
if (empty($element['#target_type'])) {
throw new \InvalidArgumentException('Missing required #target_type parameter.');
}
$storage = \Drupal::service('entity_type.manager')->getStorage($element['#target_type']);
$entityCount = $storage->getQuery()->count()->execute();
$entity_count = $storage->getQuery()->count()->execute();
$element['#tree'] = TRUE;
// No need to show anything, there's only one possible value.
if ($element['#required'] && $entityCount == 1) {
$entityIds = $storage->getQuery()->execute();
if ($element['#required'] && $entity_count == 1) {
$entity_ids = $storage->getQuery()->execute();
$element['value'] = [
'#type' => 'hidden',
'#value' => reset($entityIds),
'#value' => reset($entity_ids),
];
return $element;
}
if ($entityCount <= $element['#autocomplete_threshold']) {
if ($entity_count <= $element['#autocomplete_threshold']) {
$entities = $storage->loadMultiple();
$entityLabels = array_map(function ($entity) {
$entity_labels = array_map(function ($entity) {
return $entity->label();
}, $entities);
// Radio buttons don't have a None option by default.
if (!$element['#multiple'] && !$element['#required']) {
$entityLabels = ['' => t('None')] + $entityLabels;
$entity_labels = ['' => t('None')] + $entity_labels;
}
$element['value'] = [
'#type' => $element['#multiple'] ? 'checkboxes' : 'radios',
'#required' => $element['#required'],
'#options' => $entityLabels,
'#options' => $entity_labels,
];
if (!empty($element['#default_value'])) {
$element['value']['#default_value'] = $element['#default_value'];
}
}
else {
$defaultValue = NULL;
$default_value = NULL;
if (!empty($element['#default_value'])) {
// Upcast ids into entities, as expected by entity_autocomplete.
if ($element['#multiple']) {
$defaultValue = $storage->loadMultiple($element['#default_value']);
$default_value = $storage->loadMultiple($element['#default_value']);
}
else {
$defaultValue = $storage->load($element['#default_value']);
$default_value = $storage->load($element['#default_value']);
}
}
......@@ -122,7 +122,7 @@ class EntitySelect extends FormElement {
'#target_type' => $element['#target_type'],