|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\commerce_product\Plugin\Commerce\Condition; |
| 4 | + |
| 5 | +use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase; |
| 6 | +use Drupal\Core\Entity\EntityInterface; |
| 7 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | +use Drupal\Core\Form\FormStateInterface; |
| 9 | +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 10 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides the product condition for order items. |
| 14 | + * |
| 15 | + * @CommerceCondition( |
| 16 | + * id = "order_item_product", |
| 17 | + * label = @Translation("Product"), |
| 18 | + * display_label = @Translation("Limit by product"), |
| 19 | + * category = @Translation("Product"), |
| 20 | + * entity_type = "commerce_order_item", |
| 21 | + * ) |
| 22 | + */ |
| 23 | +class OrderItemProduct extends ConditionBase implements ContainerFactoryPluginInterface { |
| 24 | + |
| 25 | + /** |
| 26 | + * The product storage. |
| 27 | + * |
| 28 | + * @var \Drupal\Core\Entity\EntityStorageInterface |
| 29 | + */ |
| 30 | + protected $productStorage; |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructs a new OrderItemProduct object. |
| 34 | + * |
| 35 | + * @param array $configuration |
| 36 | + * The plugin configuration, i.e. an array with configuration values keyed |
| 37 | + * by configuration option name. |
| 38 | + * @param string $plugin_id |
| 39 | + * The plugin_id for the plugin instance. |
| 40 | + * @param mixed $plugin_definition |
| 41 | + * The plugin implementation definition. |
| 42 | + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
| 43 | + * The entity type manager. |
| 44 | + */ |
| 45 | + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) { |
| 46 | + parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 47 | + |
| 48 | + $this->productStorage = $entity_type_manager->getStorage('commerce_product'); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + */ |
| 54 | + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 55 | + return new static( |
| 56 | + $configuration, |
| 57 | + $plugin_id, |
| 58 | + $plugin_definition, |
| 59 | + $container->get('entity_type.manager') |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function defaultConfiguration() { |
| 67 | + return [ |
| 68 | + 'products' => [], |
| 69 | + ] + parent::defaultConfiguration(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritdoc} |
| 74 | + */ |
| 75 | + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
| 76 | + $form = parent::buildConfigurationForm($form, $form_state); |
| 77 | + |
| 78 | + $products = []; |
| 79 | + $product_ids = array_column($this->configuration['products'], 'product_id'); |
| 80 | + if (!empty($product_ids)) { |
| 81 | + $products = $this->productStorage->loadMultiple($product_ids); |
| 82 | + } |
| 83 | + $form['products'] = [ |
| 84 | + '#type' => 'entity_autocomplete', |
| 85 | + '#title' => $this->t('Products'), |
| 86 | + '#default_value' => $products, |
| 87 | + '#target_type' => 'commerce_product', |
| 88 | + '#tags' => TRUE, |
| 89 | + '#required' => TRUE, |
| 90 | + ]; |
| 91 | + |
| 92 | + return $form; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritdoc} |
| 97 | + */ |
| 98 | + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
| 99 | + parent::submitConfigurationForm($form, $form_state); |
| 100 | + |
| 101 | + $values = $form_state->getValue($form['#parents']); |
| 102 | + $this->configuration['products'] = []; |
| 103 | + foreach ($values['products'] as $value) { |
| 104 | + $this->configuration['products'][] = [ |
| 105 | + 'product_id' => $value['target_id'], |
| 106 | + ]; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * {@inheritdoc} |
| 112 | + */ |
| 113 | + public function evaluate(EntityInterface $entity) { |
| 114 | + $this->assertEntity($entity); |
| 115 | + /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ |
| 116 | + $order_item = $entity; |
| 117 | + /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $purchasable_entity */ |
| 118 | + $purchasable_entity = $order_item->getPurchasedEntity(); |
| 119 | + if (!$purchasable_entity || $purchasable_entity->getEntityTypeId() != 'commerce_product_variation') { |
| 120 | + return FALSE; |
| 121 | + } |
| 122 | + $product_ids = array_column($this->configuration['products'], 'product_id'); |
| 123 | + |
| 124 | + return in_array($purchasable_entity->getProductId(), $product_ids); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments