OrderPaymentGateway.php in Commerce Core 8.2
File
modules/payment/src/Plugin/Commerce/Condition/OrderPaymentGateway.php
View source
<?php
namespace Drupal\commerce_payment\Plugin\Commerce\Condition;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class OrderPaymentGateway extends ConditionBase {
public function defaultConfiguration() {
return [
'payment_gateways' => [],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['payment_gateways'] = [
'#type' => 'commerce_entity_select',
'#title' => $this
->t('Payment gateways'),
'#default_value' => $this->configuration['payment_gateways'],
'#target_type' => 'commerce_payment_gateway',
'#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['payment_gateways'] = $values['payment_gateways'];
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
$order = $entity;
if ($order
->get('payment_gateway')
->isEmpty()) {
return FALSE;
}
$payment_gateway_item = $order
->get('payment_gateway')
->first()
->getValue();
$payment_gateway_id = $payment_gateway_item['target_id'];
return in_array($payment_gateway_id, $this->configuration['payment_gateways']);
}
}