public static function WebformRevisionsSubmission::preCreate in Config Entity Revisions 1.x
Same name and namespace in other branches
- 8.2 modules/webform_revisions/src/Entity/WebformRevisionsSubmission.php \Drupal\webform_revisions\Entity\WebformRevisionsSubmission::preCreate()
- 8 modules/webform_revisions/src/Entity/WebformRevisionsSubmission.php \Drupal\webform_revisions\Entity\WebformRevisionsSubmission::preCreate()
Changes the values of an entity before it is created.
Load defaults for example.
Parameters
\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.
mixed[] $values: An array of values to set, keyed by property name. If the entity type has bundles the bundle key has to be specified.
Overrides WebformSubmission::preCreate
File
- modules/
webform_revisions/ src/ Entity/ WebformRevisionsSubmission.php, line 43
Class
Namespace
Drupal\webform_revisions\EntityCode
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.');
}
// Get temporary webform entity and store it in the static
// WebformSubmission::$webform property.
// This could be reworked to use \Drupal\Core\TempStore\PrivateTempStoreFactory
// but it might be overkill since we are just using this to validate
// that a webform's elements can be rendered.
// @see \Drupal\webform\WebformEntityElementsValidator::validateRendering()
// @see \Drupal\webform_ui\Form\WebformUiElementTestForm::buildForm()
if (isset($values['webform']) && $values['webform'] instanceof ConfigEntityRevisionsInterface) {
$webform = $values['webform'];
static::$webform = $values['webform'];
$values['webform_id'] = $values['webform']
->id();
}
else {
/* @var $revisionsController ConfigEntityRevisionsControllerInterface */
$revisionsController = WebformRevisionsController::create(\Drupal::getContainer());
/** @var \Drupal\webform\WebformInterface $webform */
$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;
}
// Get request's source entity parameter.
/** @var \Drupal\webform\WebformRequestInterface $request_handler */
$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,
];
// Decode all data in an array.
if (empty($values['data'])) {
$values['data'] = [];
}
elseif (is_string($values['data'])) {
$values['data'] = Yaml::decode($values['data']);
}
// Get default date from source entity 'webform' field.
if ($values['entity_type'] && $values['entity_id']) {
$source_entity = \Drupal::entityTypeManager()
->getStorage($values['entity_type'])
->load($values['entity_id']);
/** @var \Drupal\webform\WebformEntityReferenceManagerInterface $entity_reference_manager */
$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);
}
}
}
// Set default values.
$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);
}