View source
<?php
namespace Drupal\commerce_order\Plugin\Commerce\Condition;
use Drupal\commerce\EntityUuidMapperInterface;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderStore extends ConditionBase implements ContainerFactoryPluginInterface {
protected $entityUuidMapper;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUuidMapperInterface $entity_uuid_mapper) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityUuidMapper = $entity_uuid_mapper;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('commerce.entity_uuid_mapper'));
}
public function defaultConfiguration() {
return [
'stores' => [],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$store_ids = $this->entityUuidMapper
->mapToIds('commerce_store', $this->configuration['stores']);
$form['stores'] = [
'#type' => 'commerce_entity_select',
'#title' => $this
->t('Stores'),
'#default_value' => $store_ids,
'#target_type' => 'commerce_store',
'#hide_single_entity' => FALSE,
'#multiple' => TRUE,
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state
->getValue($form['#parents']);
$this->configuration['stores'] = $this->entityUuidMapper
->mapFromIds('commerce_store', $values['stores']);
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
$order = $entity;
return in_array($order
->getStore()
->uuid(), $this->configuration['stores']);
}
}