You are here

public function MappedObjectForm::submitPull 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::submitPull()

Submit handler for "pull" button.

File

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

Class

MappedObjectForm
Salesforce Mapping Form base.

Namespace

Drupal\salesforce_mapping_ui\Form

Code

public function submitPull(array &$form, FormStateInterface $form_state) {
  $mapping_id = $form_state
    ->getValue([
    'salesforce_mapping',
    0,
    'target_id',
  ]);
  $sfid = new SFID($form_state
    ->getValue([
    'salesforce_id',
    0,
    'value',
  ]));
  $mapped_object = $this->entity
    ->set('salesforce_id', (string) $sfid)
    ->set('salesforce_mapping', $mapping_id);

  // Create stub entity.
  $drupal_entity_array = $form_state
    ->getValue([
    'drupal_entity',
    0,
  ]);
  if ($drupal_entity_array['target_id']) {
    $drupal_entity = $this->entityTypeManager
      ->getStorage($drupal_entity_array['target_type'])
      ->load($drupal_entity_array['target_id']);
    $mapped_object
      ->set('drupal_entity', $drupal_entity);
  }
  else {
    $drupal_entity = $this->entityTypeManager
      ->getStorage($drupal_entity_array['target_type'])
      ->create([
      'salesforce_pull' => TRUE,
    ]);
    $mapped_object
      ->set('drupal_entity', NULL);
    $mapped_object
      ->setDrupalEntityStub($drupal_entity);
  }
  try {

    // Pull from SF. Save first to pass local validation.
    $mapped_object
      ->save();
    $mapped_object
      ->pull();
  } catch (\Exception $e) {
    $this->eventDispatcher
      ->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));
    $this
      ->messenger()
      ->addError($this
      ->t('Pull failed with an exception: %exception', [
      '%exception' => $e
        ->getMessage(),
    ]));
    $form_state
      ->setRebuild();
    return;
  }

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