collection-8.x-1.x-dev/src/ContextProvider/CollectionRouteContext.php

src/ContextProvider/CollectionRouteContext.php
<?php

namespace Drupal\collection\ContextProvider;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\collection\Entity\Collection;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Sets the current collection as a context on collection routes.
 */
class CollectionRouteContext implements ContextProviderInterface {

  use StringTranslationTrait;

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

  /**
   * Constructs a new collectionRouteContext.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $result = [];
    $context_definition = EntityContextDefinition::create('collection')->setRequired(FALSE);
    $value = NULL;

    if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['collection'])) {
      if ($collection = $this->routeMatch->getParameter('collection')) {
        $value = $collection;
      }
    }
    elseif ($this->routeMatch->getRouteName() == 'entity.collection.add_form') {
      $collection_type = $this->routeMatch->getParameter('collection_type');
      $value = Collection::create(['type' => $collection_type->id()]);
    }

    $cacheability = new CacheableMetadata();
    $cacheability->setCacheContexts(['route']);

    $context = new Context($context_definition, $value);
    $context->addCacheableDependency($cacheability);
    $result['collection'] = $context;

    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    $context = EntityContext::fromEntityTypeId('collection', $this->t('Collection from URL'));
    return ['collection' => $context];
  }

}

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

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