You are here

class QueryStringWebformSourceEntity in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformSourceEntity/QueryStringWebformSourceEntity.php \Drupal\webform\Plugin\WebformSourceEntity\QueryStringWebformSourceEntity

Detect source entity by examining query string.

Plugin annotation


@WebformSourceEntity(
  id = "query_string",
  label = @Translation("Query string"),
  weight = 0
)

Hierarchy

Expanded class hierarchy of QueryStringWebformSourceEntity

5 files declare their use of QueryStringWebformSourceEntity
QueryStringWebformSourceEntityTest.php in tests/src/Unit/Plugin/WebformSourceEntity/QueryStringWebformSourceEntityTest.php
WebformEntityReferenceLinkFormatter.php in src/Plugin/Field/FieldFormatter/WebformEntityReferenceLinkFormatter.php
WebformEntityReferenceUrlFormatter.php in src/Plugin/Field/FieldFormatter/WebformEntityReferenceUrlFormatter.php
WebformShareIframe.php in modules/webform_share/src/Element/WebformShareIframe.php
WebformShareScript.php in modules/webform_share/src/Element/WebformShareScript.php

File

src/Plugin/WebformSourceEntity/QueryStringWebformSourceEntity.php, line 26

Namespace

Drupal\webform\Plugin\WebformSourceEntity
View source
class QueryStringWebformSourceEntity extends PluginBase implements WebformSourceEntityInterface, ContainerFactoryPluginInterface {

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The webform entity reference manager.
   *
   * @var \Drupal\webform\WebformEntityReferenceManagerInterface
   */
  protected $webformEntityReferenceManager;

  /**
   * QueryStringWebformSourceEntity constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The "entity_type.manager" service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The "current_route_match" service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The "request_stack" service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The "language_manager" service.
   * @param \Drupal\webform\WebformEntityReferenceManagerInterface $webform_entity_reference_manager
   *   The "webform.entity_reference_manager" service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match, RequestStack $request_stack, LanguageManagerInterface $language_manager, WebformEntityReferenceManagerInterface $webform_entity_reference_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->routeMatch = $route_match;
    $this->request = $request_stack
      ->getCurrentRequest();
    $this->languageManager = $language_manager;
    $this->webformEntityReferenceManager = $webform_entity_reference_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('current_route_match'), $container
      ->get('request_stack'), $container
      ->get('language_manager'), $container
      ->get('webform.entity_reference_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getSourceEntity(array $ignored_types) {

    // Note: We deliberately discard $ignored_types because through query string
    // any arbitrary entity can be injected as a source.
    $webform = $this->routeMatch
      ->getParameter('webform');
    if (!$webform) {
      return NULL;
    }

    // Get and check source entity type.
    $source_entity_type = $this->request->query
      ->get('source_entity_type');
    if (!$source_entity_type || !$this->entityTypeManager
      ->hasDefinition($source_entity_type)) {
      return NULL;
    }

    // Get and check source entity id.
    $source_entity_id = $this->request->query
      ->get('source_entity_id');
    if (!$source_entity_id) {
      return NULL;
    }

    // Get and check source entity.
    $source_entity = $this->entityTypeManager
      ->getStorage($source_entity_type)
      ->load($source_entity_id);
    if (!$source_entity) {
      return NULL;
    }

    // Get translated source entity.
    if ($source_entity instanceof TranslatableInterface && $source_entity
      ->hasTranslation($this->languageManager
      ->getCurrentLanguage()
      ->getId())) {
      $source_entity = $source_entity
        ->getTranslation($this->languageManager
        ->getCurrentLanguage()
        ->getId());
    }

    // Check source entity access.
    if (!$source_entity
      ->access('view')) {
      return NULL;
    }

    // Check that the webform is referenced by the source entity.
    if (!$webform
      ->getSetting('form_prepopulate_source_entity')) {

      // Get source entity's webform field.
      $webform_field_names = $this->webformEntityReferenceManager
        ->getFieldNames($source_entity);
      foreach ($webform_field_names as $webform_field_name) {

        // Check that source entity's reference webform is the
        // current webform.
        foreach ($source_entity->{$webform_field_name} as $item) {
          if ($item->target_id === $webform
            ->id()) {
            return $source_entity;
          }
        }
      }
      return NULL;
    }
    return $source_entity;
  }

  /**
   * Get source entity route options query string parameters.
   *
   * @param \Drupal\Core\Entity\EntityInterface|null $entity
   *   An entity.
   *
   * @return array
   *   An associative array contains a source entity's route options
   *   query string parameters.
   */
  public static function getRouteOptionsQuery(EntityInterface $entity = NULL) {
    if (!$entity) {
      return [];
    }
    else {
      return [
        'query' => [
          'source_entity_type' => $entity
            ->getEntityTypeId(),
          'source_entity_id' => $entity
            ->id(),
        ],
      ];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
QueryStringWebformSourceEntity::$entityTypeManager protected property Entity type manager service.
QueryStringWebformSourceEntity::$languageManager protected property The language manager service.
QueryStringWebformSourceEntity::$request protected property The current request.
QueryStringWebformSourceEntity::$routeMatch protected property Current route match service.
QueryStringWebformSourceEntity::$webformEntityReferenceManager protected property The webform entity reference manager.
QueryStringWebformSourceEntity::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
QueryStringWebformSourceEntity::getRouteOptionsQuery public static function Get source entity route options query string parameters.
QueryStringWebformSourceEntity::getSourceEntity public function Detect and return a source entity from current context. Overrides WebformSourceEntityInterface::getSourceEntity
QueryStringWebformSourceEntity::__construct public function QueryStringWebformSourceEntity constructor. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.