OrderCustomerRole.php in Commerce Core 8.2
File
modules/order/src/Plugin/Commerce/Condition/OrderCustomerRole.php
View source
<?php
namespace Drupal\commerce_order\Plugin\Commerce\Condition;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class OrderCustomerRole extends ConditionBase {
public function defaultConfiguration() {
return [
'roles' => [],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed roles'),
'#default_value' => $this->configuration['roles'],
'#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', user_role_names()),
'#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['roles'] = array_filter($values['roles']);
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
$order = $entity;
$customer = $order
->getCustomer();
return (bool) array_intersect($this->configuration['roles'], $customer
->getRoles());
}
}