View source
<?php
namespace Drupal\salesforce_mapping_ui\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\salesforce\Event\SalesforceErrorEvent;
use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce\SFID;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class MappedObjectForm extends ContentEntityForm {
protected $mappingStorage;
protected $mappedObjectStorage;
protected $eventDispatcher;
protected $request;
protected $entityTypeManager;
public function __construct(EntityRepositoryInterface $entityRepository, EntityTypeBundleInfoInterface $entityTypeBundleInfo, TimeInterface $time, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack, EntityTypeManagerInterface $etm) {
parent::__construct($entityRepository, $entityTypeBundleInfo, $time);
$this->eventDispatcher = $event_dispatcher;
$this->request = $request_stack
->getCurrentRequest();
$this->entityTypeManager = $etm;
$this->mappingStorage = $etm
->getStorage('salesforce_mapping');
$this->mappedObjectStorage = $etm
->getStorage('salesforce_mapped_object');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('event_dispatcher'), $container
->get('request_stack'), $container
->get('entity_type.manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity_id = $entity_type_id = FALSE;
if ($this->entity
->isNew()) {
if ($drupal_entity = $this
->getDrupalEntityFromUrl()) {
$form['drupal_entity']['widget'][0]['target_type']['#default_value'] = $drupal_entity
->getEntityTypeId();
$form['drupal_entity']['widget'][0]['target_id']['#default_value'] = $drupal_entity;
}
}
$mappings = $this->mappingStorage
->loadMultiple();
if ($mappings) {
$options = array_keys($mappings);
$form['salesforce_mapping']['widget']['#options'] = array_intersect_key($form['salesforce_mapping']['widget']['#options'], array_flip($options));
}
$form['actions']['push'] = [
'#type' => 'submit',
'#value' => $this
->t('Push'),
'#weight' => 5,
'#submit' => [
[
$this,
'submitPush',
],
],
'#validate' => [
[
$this,
'validateForm',
],
[
$this,
'validatePush',
],
],
];
$form['actions']['pull'] = [
'#type' => 'submit',
'#value' => $this
->t('Pull'),
'#weight' => 6,
'#submit' => [
[
$this,
'submitPull',
],
],
'#validate' => [
[
$this,
'validateForm',
],
[
$this,
'validatePull',
],
],
];
return $form;
}
public function validatePush(array &$form, FormStateInterface $form_state) {
$drupal_entity_array = $form_state
->getValue([
'drupal_entity',
0,
]);
if (empty($drupal_entity_array['target_id'])) {
$form_state
->setErrorByName('drupal_entity][0][target_id', $this
->t('Please specify an entity to push.'));
return;
}
}
public function validatePull(array &$form, FormStateInterface $form_state) {
$sfid = $form_state
->getValue([
'salesforce_id',
0,
'value',
], FALSE);
if (!$sfid) {
$form_state
->setErrorByName('salesforce_id', $this
->t('Please specify a Salesforce ID to pull.'));
return;
}
}
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', '');
}
try {
$mapped_object
->push();
} catch (\Exception $e) {
$mapped_object
->delete();
$this->eventDispatcher
->dispatch(new SalesforceErrorEvent($e), SalesforceEvents::ERROR);
$this
->messenger()
->addError($this
->t('Push failed with an exception: %exception', [
'%exception' => $e
->getMessage(),
]));
$form_state
->setRebuild();
return;
}
$this
->messenger()
->addStatus('Push successful.');
$form_state
->setRedirect('entity.salesforce_mapped_object.canonical', [
'salesforce_mapped_object' => $mapped_object
->id(),
]);
}
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);
$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 {
$mapped_object
->save();
$mapped_object
->pull();
} catch (\Exception $e) {
$this->eventDispatcher
->dispatch(new SalesforceErrorEvent($e), SalesforceEvents::ERROR);
$this
->messenger()
->addError($this
->t('Pull failed with an exception: %exception', [
'%exception' => $e
->getMessage(),
]));
$form_state
->setRebuild();
return;
}
$this
->messenger()
->addStatus('Pull successful.');
$form_state
->setRedirect('entity.salesforce_mapped_object.canonical', [
'salesforce_mapped_object' => $mapped_object
->id(),
]);
}
public function save(array $form, FormStateInterface $form_state) {
$this
->getEntity()
->save();
$this
->messenger()
->addStatus($this
->t('The mapping has been successfully saved.'));
$form_state
->setRedirect('entity.salesforce_mapped_object.canonical', [
'salesforce_mapped_object' => $this
->getEntity()
->id(),
]);
}
private function getDrupalEntityFromUrl() {
$entity_type_id = $this->request->query
->get('entity_type_id');
$entity_id = $this->request->query
->get('entity_id');
if (empty($entity_id) || empty($entity_type_id)) {
return FALSE;
}
return $this->entityTypeManager
->getStorage($entity_type_id)
->load($entity_id);
}
}