From a85bd3d213aa6abe53d19d354f146652bfba1c5a Mon Sep 17 00:00:00 2001 From: William Hearn Date: Thu, 2 Nov 2017 17:08:25 -0400 Subject: [PATCH] Issue #2920637: Add TaxonomyRouteContext similar to NodeRouteContext --- .../src/ContextProvider/TaxonomyRouteContext.php | 74 ++++++++++++++++++++++ core/modules/taxonomy/taxonomy.services.yml | 5 ++ 2 files changed, 79 insertions(+) create mode 100644 core/modules/taxonomy/src/ContextProvider/TaxonomyRouteContext.php diff --git a/core/modules/taxonomy/src/ContextProvider/TaxonomyRouteContext.php b/core/modules/taxonomy/src/ContextProvider/TaxonomyRouteContext.php new file mode 100644 index 0000000..f8c528f --- /dev/null +++ b/core/modules/taxonomy/src/ContextProvider/TaxonomyRouteContext.php @@ -0,0 +1,74 @@ +routeMatch = $route_match; + } + + /** + * {@inheritdoc} + */ + public function getRuntimeContexts(array $unqualified_context_ids) { + $result = []; + $context_definition = new ContextDefinition('entity:taxonomy_term', NULL, FALSE); + $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; + } + } + elseif ($this->routeMatch->getRouteName() == 'taxonomy_term.add') { + $vocab = $this->routeMatch->getParameter('vocabulary'); + $value = Term::create(['type' => $vocab->id()]); + } + + $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]; + } + +} diff --git a/core/modules/taxonomy/taxonomy.services.yml b/core/modules/taxonomy/taxonomy.services.yml index a8153ed..59ec64c 100644 --- a/core/modules/taxonomy/taxonomy.services.yml +++ b/core/modules/taxonomy/taxonomy.services.yml @@ -4,3 +4,8 @@ services: arguments: ['@entity.manager'] tags: - { name: breadcrumb_builder, priority: 1002 } + taxonomy_term.taxonomy_term_route_context: + class: Drupal\taxonomy\ContextProvider\TaxonomyRouteContext + arguments: ['@current_route_match'] + tags: + - { name: 'context_provider' } -- 2.5.4 (Apple Git-61)