Guest User

Untitled

a guest
Jan 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. /**
  2. * @Condition(
  3. * id = "node_type",
  4. * label = @Translation("Node Bundle"),
  5. * context = {
  6. * "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
  7. * }
  8. * )
  9. */
  10.  
  11. /**
  12. * Return current term.
  13. */
  14. public function getCurrentTerm() {
  15. $term = null;
  16.  
  17. // Below we are using the general method to obtain the objects.
  18. // DrupaltaxonomyEntityTerm::load($tid) is the other
  19.  
  20. // Get the unaliased path in the form /taxonomy/term/tid.
  21. $current_path = Drupal::service('path.current')->getPath();
  22.  
  23. preg_match('//taxonomy/term/d+/', $current_path, $matches);
  24.  
  25. // Return null if not in proper form.
  26. if (count($matches)) {
  27. // Find the parameters for this route.
  28. $params = DrupalCoreUrl::fromUri("internal:" . $current_path)
  29. ->getRouteParameters();
  30.  
  31. // Fetch the taxonomy object.
  32. $entity_type = key($params);
  33. $term = Drupal::entityTypeManager()
  34. ->getStorage($entity_type)
  35. ->load($params[$entity_type]);
  36. }
  37.  
  38. return $term;
  39. }
  40. }
  41.  
  42. MYMODULE.taxonomy_term_context:
  43. class: DrupalMYMODULEContextProviderTaxonomyTermContext
  44. arguments: ['@current_route_match']
  45. tags:
  46. - { name: 'context_provider' }
  47.  
  48. <?php
  49.  
  50. namespace DrupalMYMODULEContextProvider;
  51.  
  52. use DrupalCoreCacheCacheableMetadata;
  53. use DrupalCorePluginContextContext;
  54. use DrupalCorePluginContextContextDefinition;
  55. use DrupalCorePluginContextContextProviderInterface;
  56. use DrupalCoreRoutingRouteMatchInterface;
  57. use DrupalCoreStringTranslationStringTranslationTrait;
  58.  
  59. /**
  60. * Sets the current taxonomy term as a context on taxonomy term routes.
  61. */
  62. class TaxonomyTermContext implements ContextProviderInterface {
  63.  
  64. use StringTranslationTrait;
  65.  
  66. /**
  67. * The route match object.
  68. *
  69. * @var DrupalCoreRoutingRouteMatchInterface
  70. */
  71. protected $routeMatch;
  72.  
  73. /**
  74. * Constructs a new object.
  75. *
  76. * @param DrupalCoreRoutingRouteMatchInterface $route_match
  77. * The route match object.
  78. */
  79. public function __construct(RouteMatchInterface $route_match) {
  80. $this->routeMatch = $route_match;
  81. }
  82.  
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getRuntimeContexts(array $unqualified_context_ids) {
  87. $result = [];
  88. $context_definition = new ContextDefinition('entity:taxonomy_term', $this->t('Taxonomy term from URL'));
  89. $value = NULL;
  90. if (
  91. ($route_object = $this->routeMatch->getRouteObject())
  92. && ($route_contexts = $route_object->getOption('parameters'))
  93. && isset($route_contexts['taxonomy_term'])
  94. ) {
  95. if ($term = $this->routeMatch->getParameter('taxonomy_term')) {
  96. $value = $term;
  97. }
  98. }
  99.  
  100. $cacheability = new CacheableMetadata();
  101. $cacheability->setCacheContexts(['route']);
  102.  
  103. $context = new Context($context_definition, $value);
  104. $context->addCacheableDependency($cacheability);
  105. $result['taxonomy_term'] = $context;
  106.  
  107. return $result;
  108. }
  109.  
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getAvailableContexts() {
  114. $context = new Context(new ContextDefinition('entity:taxonomy_term', $this->t('Taxonomy term from URL')));
  115. return ['taxonomy_term' => $context];
  116. }
  117.  
  118. }
Add Comment
Please, Sign In to add comment