You are here

class WebformRouteContext in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/ContextProvider/WebformRouteContext.php \Drupal\webform\ContextProvider\WebformRouteContext

Sets the current webform as a context on webform routes.

Hierarchy

Expanded class hierarchy of WebformRouteContext

1 string reference to 'WebformRouteContext'
webform.services.yml in ./webform.services.yml
webform.services.yml
1 service uses WebformRouteContext
webform.webform_route_context in ./webform.services.yml
Drupal\webform\ContextProvider\WebformRouteContext

File

src/ContextProvider/WebformRouteContext.php, line 16

Namespace

Drupal\webform\ContextProvider
View source
class WebformRouteContext implements ContextProviderInterface {
  use StringTranslationTrait;

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

  /**
   * Constructs a WebformRouteContext object.
   *
   * @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) {
    $context_definition = EntityContextDefinition::fromEntityTypeId('webform')
      ->setLabel(NULL)
      ->setRequired(FALSE);
    $value = NULL;
    if (($route_object = $this->routeMatch
      ->getRouteObject()) && ($route_contexts = $route_object
      ->getOption('parameters')) && isset($route_contexts['webform'])) {
      if ($webform = $this->routeMatch
        ->getParameter('webform')) {
        $value = $webform;
      }
    }
    $cacheability = new CacheableMetadata();
    $cacheability
      ->setCacheContexts([
      'route',
    ]);
    $context = new Context($context_definition, $value);
    $context
      ->addCacheableDependency($cacheability);
    return [
      'webform' => $context,
    ];
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformRouteContext::$routeMatch protected property The route match object.
WebformRouteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
WebformRouteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
WebformRouteContext::__construct public function Constructs a WebformRouteContext object.