-
-
Save clrockwell/92a79d5cc17afb9a47d1e8a7883e5465 to your computer and use it in GitHub Desktop.
Limit by product commerce condition plugin with add another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\galt\Plugin\Commerce\Condition; | |
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase; | |
use Drupal\Component\Utility\NestedArray; | |
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 product condition for order items, with add another. | |
* | |
* @CommerceCondition( | |
* id = "galt_order_item_product", | |
* label = @Translation("Product (add another)"), | |
* display_label = @Translation("Limit by product (add another)"), | |
* category = @Translation("Product"), | |
* entity_type = "commerce_order_item", | |
* ) | |
*/ | |
class GaltOrderItemProduct extends ConditionBase implements ContainerFactoryPluginInterface { | |
/** | |
* The product storage. | |
* | |
* @var \Drupal\Core\Entity\EntityStorageInterface | |
*/ | |
protected $productStorage; | |
/** | |
* Constructs a new GaltOrderItemProduct 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->productStorage = $entity_type_manager->getStorage('commerce_product'); | |
} | |
/** | |
* {@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 [ | |
'products' => [], | |
] + parent::defaultConfiguration(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
$form = parent::buildConfigurationForm($form, $form_state); | |
$form['products'] = [ | |
'#prefix' => '<div id="products-wrapper">', | |
'#suffix' => '</div>', | |
'#type' => 'fieldset', | |
'#tree' => TRUE, | |
'#title' => $this->t('Products'), | |
]; | |
$products = []; | |
$product_ids = array_column($this->configuration['products'], 'product_id'); | |
if (!empty($product_ids)) { | |
$products = $this->productStorage->loadMultiple($product_ids); | |
} | |
$productCount = $form_state->get('product_count'); | |
if (empty($productCount)) { | |
$productCount = (empty($products) ? 1 : count($products)); | |
$form_state->set('product_count', $productCount); | |
} | |
for ($delta = 0; $delta < $productCount; $delta++) { | |
$form['products'][$delta] = [ | |
'#type' => 'entity_autocomplete', | |
'#title' => $this->t('Product'), | |
'#default_value' => array_shift($products), | |
'#target_type' => 'commerce_product', | |
'#required' => ($delta === 0) ? TRUE : FALSE, | |
]; | |
} | |
$form['products']['add_item'] = [ | |
'#type' => 'submit', | |
'#value' => $this->t('Add another item'), | |
'#limit_validation_errors' => [], | |
'#submit' => [[get_class($this), 'product_add_item']], | |
'#ajax' => [ | |
'callback' => [get_class($this), 'product_ajax_callback'], | |
'wrapper' => 'products-wrapper', | |
], | |
]; | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
parent::submitConfigurationForm($form, $form_state); | |
$values = $form_state->getValue($form['#parents']); | |
$this->configuration['products'] = []; | |
foreach ($values['products'] as $key => $value) { | |
if ($key === 'add_item') { | |
continue; | |
} | |
if ($value) { | |
$this->configuration['products'][] = [ | |
'product_id' => $value, | |
]; | |
} | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function evaluate(EntityInterface $entity) { | |
$this->assertEntity($entity); | |
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ | |
$order_item = $entity; | |
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $purchasable_entity */ | |
$purchasable_entity = $order_item->getPurchasedEntity(); | |
if (!$purchasable_entity || $purchasable_entity->getEntityTypeId() != 'commerce_product_variation') { | |
return FALSE; | |
} | |
$product_ids = array_column($this->configuration['products'], 'product_id'); | |
return in_array($purchasable_entity->getProductId(), $product_ids); | |
} | |
/** | |
* Ajax callback. | |
*/ | |
public static function product_ajax_callback(&$form, FormStateInterface $form_state) { | |
$triggering_element = $form_state->getTriggeringElement(); | |
$parents = array_slice($triggering_element['#array_parents'], 0, -1); | |
return NestedArray::getValue($form, $parents); | |
} | |
/** | |
* #ajax callback: Increases product text field count by one. | |
* | |
* Assumes the existence of a 'product_count' in $form_state. | |
*/ | |
public static function product_add_item(&$form, FormStateInterface $form_state) { | |
$productCount = $form_state->get('product_count'); | |
$form_state->set('product_count', ($productCount+1)); | |
$form_state->setRebuild(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment