You are here

public static function YamlFormSubmission::preCreate in YAML Form 8

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 EntityBase::preCreate

File

src/Entity/YamlFormSubmission.php, line 540

Class

YamlFormSubmission
Defines the YamlFormSubmission entity.

Namespace

Drupal\yamlform\Entity

Code

public static function preCreate(EntityStorageInterface $storage, array &$values) {
  if (empty($values['yamlform_id']) && empty($values['yamlform'])) {
    if (empty($values['yamlform_id'])) {
      throw new \Exception('Form id (yamlform_id) is required to create a form submission.');
    }
    elseif (empty($values['yamlform'])) {
      throw new \Exception('Form (yamlform) is required to create a form submission.');
    }
  }

  // Get temporary form entity and store it in the static
  // YamlFormSubmission::$yamlform property.
  // This could be reworked to use \Drupal\user\PrivateTempStoreFactory
  // but it might be overkill since we are just using this to validate
  // that a form's elements can be rendered.
  // @see \Drupal\yamlform\YamlFormEntityElementsValidator::validateRendering()
  // @see \Drupal\yamlform_ui\Form\YamlFormUiElementTestForm::buildForm()
  if (isset($values['yamlform']) && $values['yamlform'] instanceof YamlFormInterface) {
    $yamlform = $values['yamlform'];
    self::$yamlform = $values['yamlform'];
    $values['yamlform_id'] = 'temp';
  }
  else {

    /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
    $yamlform = YamlForm::load($values['yamlform_id']);
    self::$yamlform = NULL;
  }

  // Get request's source entity parameter.

  /** @var \Drupal\yamlform\YamlFormRequestInterface $request_handler */
  $request_handler = \Drupal::service('yamlform.request');
  $source_entity = $request_handler
    ->getCurrentSourceEntity('yamlform');
  $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 'yamlform' field.
  if ($values['entity_type'] && $values['entity_id']) {
    $source_entity = \Drupal::entityTypeManager()
      ->getStorage($values['entity_type'])
      ->load($values['entity_id']);
    if ($source_entity && method_exists($source_entity, 'hasField') && $source_entity
      ->hasField('yamlform')) {
      foreach ($source_entity->yamlform as $item) {
        if ($item->target_id == $yamlform
          ->id() && $item->default_data) {
          $values['data'] += Yaml::decode($item->default_data);
        }
      }
    }
  }

  // Set default uri and remote_addr.
  $current_request = \Drupal::requestStack()
    ->getCurrentRequest();
  $values += [
    'uri' => preg_replace('#^' . base_path() . '#', '/', $current_request
      ->getRequestUri()),
    'remote_addr' => $yamlform && $yamlform
      ->isConfidential() ? '' : $current_request
      ->getClientIp(),
  ];

  // Get default uid and langcode.
  $values += [
    'uid' => \Drupal::currentUser()
      ->id(),
    'langcode' => \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId(),
  ];

  // Hard code the token.
  $values['token'] = Crypt::randomBytesBase64();

  // Set is draft.
  $values['in_draft'] = FALSE;
  $yamlform
    ->invokeHandlers(__FUNCTION__, $values);
  $yamlform
    ->invokeElements(__FUNCTION__, $values);
}