RouteParametersWebformSourceEntity.php in Webform 8.5
File
src/Plugin/WebformSourceEntity/RouteParametersWebformSourceEntity.php
View source
<?php
namespace Drupal\webform\Plugin\WebformSourceEntity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\webform\Plugin\WebformSourceEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RouteParametersWebformSourceEntity extends PluginBase implements WebformSourceEntityInterface, ContainerFactoryPluginInterface {
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_route_match'));
}
public function getSourceEntity(array $ignored_types) {
if ($this->routeMatch
->getRouteName() === 'entity.webform_submission.user') {
return NULL;
}
$parameters = $this->routeMatch
->getParameters()
->all();
$parameters = array_reverse($parameters);
if (!empty($ignored_types)) {
$parameters = array_diff_key($parameters, array_flip($ignored_types));
}
foreach ($parameters as $value) {
if ($value instanceof EntityInterface) {
return $value;
}
}
return NULL;
}
}