CurrentUserCartIds.php in Commerce Core 8.2
File
modules/cart/src/Plugin/views/argument_default/CurrentUserCartIds.php
View source
<?php
namespace Drupal\commerce_cart\Plugin\views\argument_default;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrentUserCartIds extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
protected $cartProvider;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->cartProvider = $container
->get('commerce_cart.cart_provider');
return $instance;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['argument'] = [
'#markup' => '<div class="description">' . $this
->t("Provides the ID of the current user's first non-empty cart for the current store by default.") . '<br />' . $this
->t("Check <em>Allow multiple values</em> under <em>More</em> to provide all of the matching cart IDs for the current store.") . '</div>',
];
return $form;
}
public function getArgument() {
$carts = array_filter($this->cartProvider
->getCarts(), function ($cart) {
return $cart
->hasItems();
});
return implode('+', array_keys($carts));
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [
'cart',
];
}
}