commerce_product_bundles-8.x-1.0/src/ContextProvider/BundleVariationContext.php

src/ContextProvider/BundleVariationContext.php
<?php

namespace Drupal\commerce_product_bundles\ContextProvider;

use Drupal\commerce_product_bundles\Entity\ProductBundle;
use Drupal\commerce_product_bundles\Entity\ProductBundleType;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\layout_builder\DefaultsSectionStorageInterface;
use Drupal\layout_builder\OverridesSectionStorageInterface;

/**
 * Class BundleVariationContext
 *
 * Code was taken from and modified:
 * @see \Drupal\commerce_product\ContextProvider\ProductVariationContext
 *
 * @package Drupal\commerce_product_bundles\ContextProvider
 */
class BundleVariationContext implements ContextProviderInterface {

  use StringTranslationTrait;

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

  /**
   * The bundle variation storage.
   *
   * @var \Drupal\commerce_product_bundles\ProductBundleVariationStorageInterface
   */
  protected $bundleVariationStorage;

  /**
   * BundleVariationContext constructor.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager) {
    $this->routeMatch = $route_match;
    $this->bundleVariationStorage = $entity_type_manager->getStorage('commerce_bundle_variation');
  }

  /**
   * {@inheritDoc}
   *
   * @param array $unqualified_context_ids
   *
   * @return \Drupal\Core\Plugin\Context\Context[]
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $context_definition = new EntityContextDefinition('entity:commerce_bundle_variation', new TranslatableMarkup('Bundle variation'));
    $value = $this->routeMatch->getParameter('commerce_bundle_variation');
    if ($value === NULL) {
      if ($bundle_product = $this->routeMatch->getParameter('commerce_product_bundles')) {
        $value = $this->bundleVariationStorage->loadFromContext($bundle_product);
      }
      /** @var \Drupal\commerce_product_bundles\Entity\ProductBundleType $product_bundle_type */
      elseif ($product_bundle_type = $this->routeMatch->getParameter('commerce_product_bundles_type')) {
        if (is_string($product_bundle_type)) {
          $product_bundle_type = ProductBundleType::load($product_bundle_type);
        }
        $value = $this->bundleVariationStorage->createWithSampleValues($product_bundle_type->getBundleVariationTypeId());
      }
      elseif (strpos($this->routeMatch->getRouteName(), 'layout_builder') !== FALSE) {
        /** @var \Drupal\layout_builder\SectionStorageInterface $section_storage */
        $section_storage = $this->routeMatch->getParameter('section_storage');
        if ($section_storage instanceof DefaultsSectionStorageInterface) {
          $context = $section_storage->getContextValue('display');
          assert($context instanceof EntityDisplayInterface);
          if ($context->getTargetEntityTypeId() === 'commerce_product_bundles') {
            $product_bundle_type = ProductBundleType::load($context->getTargetBundle());
            $value = $this->bundleVariationStorage->createWithSampleValues($product_bundle_type->getBundleVariationTypeId());
          }
        }
        elseif ($section_storage instanceof OverridesSectionStorageInterface) {
          $context = $section_storage->getContextValue('entity');
          if ($context instanceof ProductBundle) {
            $value = $context->getDefaultVariation();
            if ($value === NULL) {
              $product_bundle_type = ProductBundleType::load($context->bundle());
              $value = $this->bundleVariationStorage->createWithSampleValues($product_bundle_type->getBundleVariationTypeId());
            }
          }
        }
      }
    }

    $cacheability = new CacheableMetadata();
    $cacheability->setCacheContexts(['route']);
    $context = new Context($context_definition, $value);
    $context->addCacheableDependency($cacheability);

    return ['commerce_bundle_variation' => $context];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    return $this->getRuntimeContexts([]);
  }

}

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

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