commerce_product_bundles-8.x-1.0/src/Plugin/views/argument_default/ProductBundle.php

src/Plugin/views/argument_default/ProductBundle.php
<?php

namespace Drupal\commerce_product_bundles\Plugin\views\argument_default;

use Drupal\commerce_product_bundles\Entity\ProductBundleInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Default argument plugin to extract a product bundle.
 *
 *  Code was taken form and modified:
 * @see \Drupal\commerce_product\Plugin\views\argument_default\Product
 *
 * @ViewsArgumentDefault(
 *   id = "product_bundle",
 *   title = @Translation("Product Bundle ID from URL")
 * )
 */
class ProductBundle extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * ProductBundle constructor.
   *
   * @param array $configuration
   * @param $plugin_id
   * @param $plugin_definition
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_route_match')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getArgument() {
    if (($product_bundle = $this->routeMatch->getParameter('commerce_product_bundles')) && $product_bundle instanceof ProductBundleInterface) {
      return $product_bundle->id();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return ['url'];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc