You are here

WebformSubmissionRouteContext.php in Webform 6.x

Same filename and directory in other branches
  1. 8.5 src/ContextProvider/WebformSubmissionRouteContext.php

File

src/ContextProvider/WebformSubmissionRouteContext.php
View source
<?php

namespace Drupal\webform\ContextProvider;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Sets the current webform submission as a context on webform submission routes.
 */
class WebformSubmissionRouteContext implements ContextProviderInterface {
  use StringTranslationTrait;

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

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

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

}

Classes

Namesort descending Description
WebformSubmissionRouteContext Sets the current webform submission as a context on webform submission routes.