YamlFormRequest.php in YAML Form 8
File
src/YamlFormRequest.php
View source
<?php
namespace Drupal\yamlform;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class YamlFormRequest implements YamlFormRequestInterface {
protected $entityTypeManager;
protected $entityTypeRepository;
protected $routeMatch;
protected $request;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, RequestStack $request_stack, RouteMatchInterface $route_match) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeRepository = $entity_type_repository;
$this->request = $request_stack
->getCurrentRequest();
$this->routeMatch = $route_match;
}
public function getCurrentSourceEntity($ignored_types = NULL) {
if ($source_entity = $this
->getCurrentSourceEntityFromQuery()) {
return $source_entity;
}
$entity_types = $this->entityTypeRepository
->getEntityTypeLabels();
if ($ignored_types) {
if (is_array($ignored_types)) {
$entity_types = array_diff_key($entity_types, array_flip($ignored_types));
}
else {
unset($entity_types[$ignored_types]);
}
}
foreach ($entity_types as $entity_type => $entity_label) {
$entity = $this->routeMatch
->getParameter($entity_type);
if ($entity instanceof EntityInterface) {
return $entity;
}
}
return NULL;
}
public function getCurrentYamlForm() {
$source_entity = self::getCurrentSourceEntity('yamlform');
if ($source_entity && method_exists($source_entity, 'hasField') && $source_entity
->hasField('yamlform')) {
return $source_entity->yamlform->entity;
}
else {
return $this->routeMatch
->getParameter('yamlform');
}
}
public function getYamlFormEntities() {
$yamlform = $this
->getCurrentYamlForm();
$source_entity = $this
->getCurrentSourceEntity('yamlform');
return [
$yamlform,
$source_entity,
];
}
public function getYamlFormSubmissionEntities() {
$yamlform_submission = $this->routeMatch
->getParameter('yamlform_submission');
$source_entity = $this
->getCurrentSourceEntity('yamlform_submission');
return [
$yamlform_submission,
$source_entity,
];
}
public function getRouteName(EntityInterface $yamlform_entity, EntityInterface $source_entity = NULL, $route_name) {
return $this
->getBaseRouteName($yamlform_entity, $source_entity) . '.' . $route_name;
}
public function getRouteParameters(EntityInterface $yamlform_entity, EntityInterface $source_entity = NULL) {
if (self::isValidSourceEntity($yamlform_entity, $source_entity)) {
if ($yamlform_entity instanceof YamlFormSubmissionInterface) {
return [
'yamlform_submission' => $yamlform_entity
->id(),
$source_entity
->getEntityTypeId() => $source_entity
->id(),
];
}
else {
return [
$source_entity
->getEntityTypeId() => $source_entity
->id(),
];
}
}
elseif ($yamlform_entity instanceof YamlFormSubmissionInterface) {
return [
'yamlform_submission' => $yamlform_entity
->id(),
'yamlform' => $yamlform_entity
->getYamlForm()
->id(),
];
}
else {
return [
$yamlform_entity
->getEntityTypeId() => $yamlform_entity
->id(),
];
}
}
public function getBaseRouteName(EntityInterface $yamlform_entity, EntityInterface $source_entity = NULL) {
if ($yamlform_entity instanceof YamlFormSubmissionInterface) {
$yamlform = $yamlform_entity
->getYamlForm();
}
elseif ($yamlform_entity instanceof YamlFormInterface) {
$yamlform = $yamlform_entity;
}
else {
throw new \InvalidArgumentException('Form entity');
}
if (self::isValidSourceEntity($yamlform, $source_entity)) {
return 'entity.' . $source_entity
->getEntityTypeId();
}
else {
return 'entity';
}
}
public function isValidSourceEntity(EntityInterface $yamlform_entity, EntityInterface $source_entity = NULL) {
if ($yamlform_entity instanceof YamlFormSubmissionInterface) {
$yamlform = $yamlform_entity
->getYamlForm();
}
elseif ($yamlform_entity instanceof YamlFormInterface) {
$yamlform = $yamlform_entity;
}
else {
throw new \InvalidArgumentException('Form entity');
}
if ($source_entity && method_exists($source_entity, 'hasField') && $source_entity
->hasField('yamlform') && $source_entity->yamlform->target_id == $yamlform
->id()) {
return TRUE;
}
else {
return FALSE;
}
}
protected function getCurrentSourceEntityFromQuery() {
$yamlform = $this->routeMatch
->getParameter('yamlform');
if (!$yamlform) {
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
->access('view')) {
return NULL;
}
if (!$yamlform
->getSetting('form_prepopulate_source_entity')) {
$yamlform_field_name = $this
->getSourceEntityYamlFormFieldName($source_entity);
if (!$yamlform_field_name) {
return NULL;
}
if ($source_entity->{$yamlform_field_name}->target_id != $yamlform
->id()) {
return NULL;
}
}
return $source_entity;
}
protected function getSourceEntityYamlFormFieldName(EntityInterface $source_entity) {
if ($source_entity instanceof ContentEntityInterface) {
$fields = $source_entity
->getFieldDefinitions();
foreach ($fields as $field_name => $field_definition) {
if ($field_definition
->getType() == 'yamlform') {
return $field_name;
}
}
}
return '';
}
}