View source
<?php
namespace Drupal\webform;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\UserDataInterface;
use Drupal\webform\Entity\Webform;
class WebformEntityReferenceManager implements WebformEntityReferenceManagerInterface {
protected $routeMatch;
protected $currentUser;
protected $userData;
protected $moduleHandler;
protected $entityTypeManager;
protected $webforms = [];
protected $fieldNames = [];
public function __construct(RouteMatchInterface $route_match, AccountInterface $current_user, UserDataInterface $user_data, ModuleHandlerInterface $module_handler = NULL, EntityTypeManagerInterface $entity_type_manager = NULL) {
$this->routeMatch = $route_match;
$this->currentUser = $current_user;
$this->userData = $user_data;
$this->moduleHandler = $module_handler ?: \Drupal::moduleHandler();
$this->entityTypeManager = $entity_type_manager ?: \Drupal::entityTypeManager();
}
public function isUserWebformRoute(EntityInterface $entity) {
$entity_type = $entity
->getEntityTypeId();
$route_name = $this->routeMatch
->getRouteName();
$user_routes = [
"entity.{$entity_type}.webform.test_form",
"entity.{$entity_type}.webform.api_form",
];
return in_array($this->routeMatch
->getRouteName(), $user_routes) || strpos($route_name, "entity.{$entity_type}.webform.results_") === 0 || strpos($route_name, "entity.{$entity_type}.webform.share_") === 0;
}
public function setUserWebformId(EntityInterface $entity, $webform_id) {
$module = 'webform_' . $entity
->getEntityTypeId();
$uid = $this->currentUser
->id();
$name = $entity
->id();
$values = $this->userData
->get($module, $uid, $name) ?: [];
$values['target_id'] = $webform_id;
$this->userData
->set($module, $uid, $name, $values);
}
public function getUserWebformId(EntityInterface $entity) {
$module = 'webform_' . $entity
->getEntityTypeId();
$uid = $this->currentUser
->id();
$name = $entity
->id();
$values = $this->userData
->get($module, $uid, $name) ?: [];
if (isset($values['target_id'])) {
$webforms = $this
->getWebforms($entity);
if (isset($webforms[$values['target_id']])) {
return $values['target_id'];
}
}
return NULL;
}
public function deleteUserWebformId(EntityInterface $entity) {
$module = 'webform_' . $entity
->getEntityTypeId();
$name = $entity
->id();
$this->userData
->delete($module, NULL, $name);
}
public function hasField(EntityInterface $entity = NULL) {
return $this
->getFieldName($entity) ? TRUE : FALSE;
}
public function getFieldName(EntityInterface $entity = NULL) {
$field_names = $this
->getFieldNames($entity);
return $field_names ? reset($field_names) : '';
}
public function getFieldNames(EntityInterface $entity = NULL) {
if ($entity === NULL || !$entity instanceof FieldableEntityInterface) {
return [];
}
$entity_id = $entity
->getEntityTypeId() . '-' . $entity
->id();
if (isset($this->fieldNames[$entity_id])) {
return $this->fieldNames[$entity_id];
}
$field_names = [];
if ($entity instanceof ContentEntityInterface) {
$fields = $entity
->getFieldDefinitions();
foreach ($fields as $field_name => $field_definition) {
if ($field_definition
->getType() === 'webform') {
$field_names[$field_name] = $field_name;
}
}
}
ksort($field_names);
$this->fieldNames[$entity_id] = $field_names;
return $field_names;
}
public function getWebform(EntityInterface $entity = NULL) {
if ($webform_id = $this
->getUserWebformId($entity)) {
return Webform::load($webform_id);
}
elseif ($webforms = $this
->getWebforms($entity)) {
return reset($webforms);
}
else {
return NULL;
}
}
public function getWebforms(EntityInterface $entity = NULL) {
$entity_id = $entity
->getEntityTypeId() . '-' . $entity
->id();
if (isset($this->webforms[$entity_id])) {
return $this->webforms[$entity_id];
}
$target_entities = [];
$sorted_entities = [];
$field_names = $this
->getFieldNames($entity);
foreach ($field_names as $field_name) {
foreach ($entity->{$field_name} as $item) {
if ($item->entity) {
$sorted_entities[$item->target_id] = method_exists($item->entity, 'getWeight') ? $item->entity
->getWeight() : 0;
$target_entities[$item->target_id] = $item->entity;
}
}
}
$this
->getParagraphWebformsRecursive($entity, $target_entities, $sorted_entities);
ksort($sorted_entities);
asort($sorted_entities, SORT_NUMERIC);
$webforms = [];
foreach (array_keys($sorted_entities) as $target_id) {
$webforms[$target_id] = $target_entities[$target_id];
}
$this->webforms[$entity_id] = $webforms;
return $webforms;
}
protected function getParagraphWebformsRecursive(EntityInterface $entity, array &$target_entities, array &$sorted_entities) {
if (!$this->moduleHandler
->moduleExists('paragraphs')) {
return;
}
if ($entity === NULL || !$entity instanceof FieldableEntityInterface) {
return;
}
$paragraph_fields = $this
->getParagraphFieldNames($entity);
foreach ($paragraph_fields as $paragraph_field) {
if (!$entity
->hasField($paragraph_field)) {
continue;
}
foreach ($entity
->get($paragraph_field) as $paragraph_item) {
$paragraph = $paragraph_item->entity;
if ($paragraph) {
$webform_field_names = $this
->getFieldNames($paragraph);
foreach ($webform_field_names as $webform_field_name) {
foreach ($paragraph->{$webform_field_name} as $webform_field_item) {
if ($webform_field_item->entity) {
$sorted_entities[$webform_field_item->target_id] = method_exists($webform_field_item->entity, 'getWeight') ? $webform_field_item->entity
->getWeight() : 0;
$target_entities[$webform_field_item->target_id] = $webform_field_item->entity;
}
}
}
$this
->getParagraphWebformsRecursive($paragraph, $target_entities, $sorted_entities);
}
}
}
}
protected function getParagraphFieldNames(EntityInterface $entity) {
$fields = $this->entityTypeManager
->getStorage('field_storage_config')
->loadByProperties([
'entity_type' => $entity
->getEntityTypeId(),
'type' => 'entity_reference_revisions',
]);
$field_names = [];
foreach ($fields as $field) {
if ($field
->getSetting('target_type') === 'paragraph') {
$field_name = $field
->get('field_name');
$field_names[$field_name] = $field_name;
}
}
return $field_names;
}
public function getTableNames() {
$field_storage_configs = FieldStorageConfig::loadMultiple();
$tables = [];
foreach ($field_storage_configs as $field_storage_config) {
if ($field_storage_config
->getType() === 'webform') {
$webform_field_table = $field_storage_config
->getTargetEntityTypeId();
$webform_field_name = $field_storage_config
->getName();
$tables[$webform_field_table . '__' . $webform_field_name] = $webform_field_name;
$tables[$webform_field_table . '_revision__' . $webform_field_name] = $webform_field_name;
}
}
return $tables;
}
}