routeMatch = $route_match; } /** * {@inheritdoc} */ public function getRuntimeContexts(array $unqualified_context_ids) { $context_definition = EntityContextDefinition::fromEntityTypeId('media')->setLabel(NULL)->setRequired(FALSE); $value = NULL; // Hack the media out of the route. $route_object = $this->routeMatch->getRouteObject(); if ($route_object) { $route_contexts = $route_object->getOption('parameters'); if ($route_contexts && isset($route_contexts['media'])) { $media = $this->routeMatch->getParameter('media'); if ($media) { $value = $media; } } elseif ($this->routeMatch->getRouteName() == 'entity.media.add_form') { $media_type = $this->routeMatch->getParameter('media_type'); $value = Media::create(['bundle' => $media_type->id()]); } } $cacheability = new CacheableMetadata(); $cacheability->setCacheContexts(['route']); $context = new Context($context_definition, $value); $context->addCacheableDependency($cacheability); return ['media' => $context]; } /** * {@inheritdoc} */ public function getAvailableContexts() { $context = EntityContext::fromEntityType(\Drupal::entityTypeManager()->getDefinition('media'), $this->t('Media from URL')); return ['media' => $context]; } }