Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @Condition(
- * id = "node_type",
- * label = @Translation("Node Bundle"),
- * context = {
- * "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
- * }
- * )
- */
- /**
- * Return current term.
- */
- public function getCurrentTerm() {
- $term = null;
- // Below we are using the general method to obtain the objects.
- // DrupaltaxonomyEntityTerm::load($tid) is the other
- // Get the unaliased path in the form /taxonomy/term/tid.
- $current_path = Drupal::service('path.current')->getPath();
- preg_match('//taxonomy/term/d+/', $current_path, $matches);
- // Return null if not in proper form.
- if (count($matches)) {
- // Find the parameters for this route.
- $params = DrupalCoreUrl::fromUri("internal:" . $current_path)
- ->getRouteParameters();
- // Fetch the taxonomy object.
- $entity_type = key($params);
- $term = Drupal::entityTypeManager()
- ->getStorage($entity_type)
- ->load($params[$entity_type]);
- }
- return $term;
- }
- }
- MYMODULE.taxonomy_term_context:
- class: DrupalMYMODULEContextProviderTaxonomyTermContext
- arguments: ['@current_route_match']
- tags:
- - { name: 'context_provider' }
- <?php
- namespace DrupalMYMODULEContextProvider;
- use DrupalCoreCacheCacheableMetadata;
- use DrupalCorePluginContextContext;
- use DrupalCorePluginContextContextDefinition;
- use DrupalCorePluginContextContextProviderInterface;
- use DrupalCoreRoutingRouteMatchInterface;
- use DrupalCoreStringTranslationStringTranslationTrait;
- /**
- * Sets the current taxonomy term as a context on taxonomy term routes.
- */
- class TaxonomyTermContext implements ContextProviderInterface {
- use StringTranslationTrait;
- /**
- * The route match object.
- *
- * @var DrupalCoreRoutingRouteMatchInterface
- */
- protected $routeMatch;
- /**
- * Constructs a new object.
- *
- * @param DrupalCoreRoutingRouteMatchInterface $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 = new ContextDefinition('entity:taxonomy_term', $this->t('Taxonomy term from URL'));
- $value = NULL;
- if (
- ($route_object = $this->routeMatch->getRouteObject())
- && ($route_contexts = $route_object->getOption('parameters'))
- && isset($route_contexts['taxonomy_term'])
- ) {
- if ($term = $this->routeMatch->getParameter('taxonomy_term')) {
- $value = $term;
- }
- }
- $cacheability = new CacheableMetadata();
- $cacheability->setCacheContexts(['route']);
- $context = new Context($context_definition, $value);
- $context->addCacheableDependency($cacheability);
- $result['taxonomy_term'] = $context;
- return $result;
- }
- /**
- * {@inheritdoc}
- */
- public function getAvailableContexts() {
- $context = new Context(new ContextDefinition('entity:taxonomy_term', $this->t('Taxonomy term from URL')));
- return ['taxonomy_term' => $context];
- }
- }
Add Comment
Please, Sign In to add comment