You are here

public function MappedObjectForm::submitPush in Salesforce Suite 8.3

Submit handler for "push" button.

File

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

Class

MappedObjectForm
Salesforce Mapping Form base.

Namespace

Drupal\salesforce_mapping\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));
    drupal_set_message(t('Push failed with an exception: %exception', [
      '%exception' => $e
        ->getMessage(),
    ]), 'error');
    $form_state
      ->setRebuild();
    return;
  }

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