You are here

public function MappedObjectForm::submitPush in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 5.0.x modules/salesforce_mapping_ui/src/Form/MappedObjectForm.php \Drupal\salesforce_mapping_ui\Form\MappedObjectForm::submitPush()

Submit handler for "push" button.

File

modules/salesforce_mapping_ui/src/Form/MappedObjectForm.php, line 177

Class

MappedObjectForm
Salesforce Mapping Form base.

Namespace

Drupal\salesforce_mapping_ui\Form

Code

public function submitPush(array &$form, FormStateInterface $form_state) {
  $drupal_entity_array = $form_state
    ->getValue([
    'drupal_entity',
    0,
  ]);
  $mapped_object = $this->entity;
  $mapped_object
    ->set('drupal_entity', $drupal_entity_array)
    ->set('salesforce_mapping', $form_state
    ->getValue([
    'salesforce_mapping',
    0,
    'target_id',
  ]));
  if ($sfid = $form_state
    ->getValue([
    'salesforce_id',
    0,
    'value',
  ], FALSE)) {
    $mapped_object
      ->set('salesforce_id', (string) new SFID($sfid));
  }
  else {
    $mapped_object
      ->set('salesforce_id', '');
  }

  // Push to SF.
  try {

    // Push calls save(), so this is all we need to do:
    $mapped_object
      ->push();
  } catch (\Exception $e) {
    $mapped_object
      ->delete();
    $this->eventDispatcher
      ->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));
    $this
      ->messenger()
      ->addError($this
      ->t('Push failed with an exception: %exception', [
      '%exception' => $e
        ->getMessage(),
    ]));
    $form_state
      ->setRebuild();
    return;
  }

  // @TODO: more verbose feedback for successful push.
  $this
    ->messenger()
    ->addStatus('Push successful.');
  $form_state
    ->setRedirect('entity.salesforce_mapped_object.canonical', [
    'salesforce_mapped_object' => $mapped_object
      ->id(),
  ]);
}