QueryStringWebformSourceEntity.php in Webform 8.5
File
src/Plugin/WebformSourceEntity/QueryStringWebformSourceEntity.php
View source
<?php
namespace Drupal\webform\Plugin\WebformSourceEntity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\webform\Plugin\WebformSourceEntityInterface;
use Drupal\webform\WebformEntityReferenceManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class QueryStringWebformSourceEntity extends PluginBase implements WebformSourceEntityInterface, ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $routeMatch;
protected $request;
protected $languageManager;
protected $webformEntityReferenceManager;
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;
}
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'));
}
public function getSourceEntity(array $ignored_types) {
$webform = $this->routeMatch
->getParameter('webform');
if (!$webform) {
return NULL;
}
$source_entity_type = $this->request->query
->get('source_entity_type');
if (!$source_entity_type || !$this->entityTypeManager
->hasDefinition($source_entity_type)) {
return NULL;
}
$source_entity_id = $this->request->query
->get('source_entity_id');
if (!$source_entity_id) {
return NULL;
}
$source_entity = $this->entityTypeManager
->getStorage($source_entity_type)
->load($source_entity_id);
if (!$source_entity) {
return NULL;
}
if ($source_entity instanceof TranslatableInterface && $source_entity
->hasTranslation($this->languageManager
->getCurrentLanguage()
->getId())) {
$source_entity = $source_entity
->getTranslation($this->languageManager
->getCurrentLanguage()
->getId());
}
if (!$source_entity
->access('view')) {
return NULL;
}
if (!$webform
->getSetting('form_prepopulate_source_entity')) {
$webform_field_names = $this->webformEntityReferenceManager
->getFieldNames($source_entity);
foreach ($webform_field_names as $webform_field_name) {
foreach ($source_entity->{$webform_field_name} as $item) {
if ($item->target_id === $webform
->id()) {
return $source_entity;
}
}
}
return NULL;
}
return $source_entity;
}
public static function getRouteOptionsQuery(EntityInterface $entity = NULL) {
if (!$entity) {
return [];
}
else {
return [
'query' => [
'source_entity_type' => $entity
->getEntityTypeId(),
'source_entity_id' => $entity
->id(),
],
];
}
}
}