Skip to content

Instantly share code, notes, and snippets.

@sumanthkumarc
Created July 7, 2017 11:53
Show Gist options
  • Select an option

  • Save sumanthkumarc/dd0700709504c45aeb3eb1050462d2fe to your computer and use it in GitHub Desktop.

Select an option

Save sumanthkumarc/dd0700709504c45aeb3eb1050462d2fe to your computer and use it in GitHub Desktop.
Drupal 8: An example plugin for conditions API Commerce 2.x
<?php
namespace Drupal\commerce_price\Plugin\Commerce\Condition;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the currency condition for orders.
*
* @CommerceCondition(
* id = "order_currency",
* label = @Translation("Currency"),
* display_label = @Translation("Limit by currency"),
* category = @Translation("Order"),
* entity_type = "commerce_order",
* )
*/
class OrderCurrency extends ConditionBase implements ContainerFactoryPluginInterface {
/**
* The currency storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $currencyStorage;
/**
* Constructs a new OrderCurrency object.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currencyStorage = $entity_type_manager->getStorage('commerce_currency');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'currencies' => [],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$active_currencies = $this->currencyStorage
->loadByProperties([
'status' => TRUE,
]);
// If no currency is imported or active, return no form.
if (empty($active_currencies)) {
return $form;
}
foreach ($active_currencies as $currency_code => $currency) {
/* @var \Drupal\commerce_price\Entity\Currency $currency */
$currency_list[$currency_code] = $currency->getName();
}
$selected_currencies = $this->configuration['currencies'];
$form['currencies'] = [
'#type' => 'select',
'#title' => $this->t('Currencies'),
'#multiple' => TRUE,
'#default_value' => $selected_currencies,
'#options' => $currency_list,
'#required' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state->getValue($form['#parents']);
$this->configuration['currencies'] = $values['currencies'];
}
/**
* {@inheritdoc}
*/
public function evaluate(EntityInterface $entity) {
$this->assertEntity($entity);
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $entity;
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $purchasable_entity */
$order_currency = $order->getTotalPrice()->getCurrencyCode();
$selected_currencies = $this->configuration['currencies'];
return in_array($order_currency, $selected_currencies) ? TRUE : FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment