You are here

public function YamlFormSubmissionForm::setEntity in YAML Form 8

Sets the form entity.

Sets the form entity which will be used for populating form element defaults. Usually, the form entity gets updated by \Drupal\Core\Entity\EntityFormInterface::submit(), however this may be used to completely exchange the form entity, e.g. when preparing the rebuild of a multi-step form.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the current form should operate upon.

Return value

$this

Overrides EntityForm::setEntity

File

src/YamlFormSubmissionForm.php, line 131

Class

YamlFormSubmissionForm
Provides a form to collect and edit submissions.

Namespace

Drupal\yamlform

Code

public function setEntity(EntityInterface $entity) {

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
  $yamlform = $entity
    ->getYamlForm();
  $this->sourceEntity = $this->requestHandler
    ->getCurrentSourceEntity([
    'yamlform',
    'yamlform_submission',
  ]);
  if ($yamlform
    ->getSetting('token_update') && ($token = $this
    ->getRequest()->query
    ->get('token'))) {
    if ($yamlform_submissions_token = $this->storage
      ->loadByProperties([
      'token' => $token,
    ])) {
      $entity = reset($yamlform_submissions_token);
    }
  }
  elseif ($yamlform_submission_draft = $this->storage
    ->loadDraft($yamlform, $this->sourceEntity, $this
    ->currentUser())) {
    $entity = $yamlform_submission_draft;
  }
  $this->messageManager
    ->setYamlFormSubmission($entity);
  $this->messageManager
    ->setSourceEntity($this->sourceEntity);
  return parent::setEntity($entity);
}