WebformRevisionsSubmission.php in Config Entity Revisions 8
File
modules/webform_revisions/src/Entity/WebformRevisionsSubmission.php
View source
<?php
namespace Drupal\webform_revisions\Entity;
use Drupal\config_entity_revisions\ConfigEntityRevisionsInterface;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform_revisions\WebformRevisionsConfigTrait;
use Drupal\config_entity_revisions\ConfigEntityRevisionsConfigTrait;
use Drupal\webform\Entity\Webform;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\webform_revisions\Controller\WebformRevisionsController;
use Drupal\Component\Utility\Crypt;
class WebformRevisionsSubmission extends WebformSubmission {
public function getWebform() {
if ($this->webform) {
return $this->webform;
}
$webform_id = $this->webform_id->target_id;
$revision_id = $this->webform_revision[0];
if ($revision_id) {
$revision_id = $revision_id->target_id;
$revisionsController = WebformRevisionsController::create(\Drupal::getContainer());
$webform = $revisionsController
->loadConfigEntityRevision($revision_id, $webform_id);
return $webform;
}
if (isset($this->webform_id->entity)) {
return $this->webform_id->entity;
}
else {
return static::$webform;
}
}
public static function preCreate(EntityStorageInterface $storage, array &$values) {
if (empty($values['webform_id']) && empty($values['webform'])) {
throw new \Exception('A webform id or instance is required to create a webform submission.');
}
if (isset($values['webform']) && $values['webform'] instanceof ConfigEntityRevisionsInterface) {
$webform = $values['webform'];
static::$webform = $values['webform'];
$values['webform_id'] = $values['webform']
->id();
}
else {
$revisionsController = WebformRevisionsController::create(\Drupal::getContainer());
$webform = $revisionsController
->loadConfigEntityRevision();
if (!empty($values['webform_id']) && (!$webform || $values['webform_id'] !== $webform
->id())) {
$webform = \Drupal::entityTypeManager()
->getStorage('webform')
->load($values['webform_id']);
}
static::$webform = NULL;
}
$request_handler = \Drupal::service('webform.request');
$source_entity = $request_handler
->getCurrentSourceEntity('webform');
$values += [
'entity_type' => $source_entity ? $source_entity
->getEntityTypeId() : NULL,
'entity_id' => $source_entity ? $source_entity
->id() : NULL,
];
if (empty($values['data'])) {
$values['data'] = [];
}
elseif (is_string($values['data'])) {
$values['data'] = Yaml::decode($values['data']);
}
if ($values['entity_type'] && $values['entity_id']) {
$source_entity = \Drupal::entityTypeManager()
->getStorage($values['entity_type'])
->load($values['entity_id']);
$entity_reference_manager = \Drupal::service('webform.entity_reference_manager');
if ($webform_field_name = $entity_reference_manager
->getFieldName($source_entity)) {
if ($source_entity->{$webform_field_name}->target_id == $webform
->id() && $source_entity->{$webform_field_name}->default_data) {
$values['data'] += Yaml::decode($source_entity->{$webform_field_name}->default_data);
}
}
}
$current_request = \Drupal::requestStack()
->getCurrentRequest();
$values += [
'in_draft' => FALSE,
'uid' => \Drupal::currentUser()
->id(),
'langcode' => \Drupal::languageManager()
->getCurrentLanguage()
->getId(),
'token' => Crypt::randomBytesBase64(),
'uri' => preg_replace('#^' . base_path() . '#', '/', $current_request
->getRequestUri()),
'remote_addr' => $webform && $webform
->isConfidential() ? '' : $current_request
->getClientIp(),
];
$webform
->invokeHandlers(__FUNCTION__, $values);
$webform
->invokeElements(__FUNCTION__, $values);
}
}