SalesforceMappingFormBase.php in Salesforce Suite 8.4
File
modules/salesforce_mapping_ui/src/Form/SalesforceMappingFormBase.php
View source
<?php
namespace Drupal\salesforce_mapping_ui\Form;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Url;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginManager;
use Drupal\salesforce_mapping\SalesforceMappableEntityTypesInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\salesforce\Rest\RestClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class SalesforceMappingFormBase extends EntityForm {
protected $mappingFieldPluginManager;
protected $client;
protected $mappableEntityTypes;
protected $entity;
protected $bundleInfo;
public function __construct(SalesforceMappingFieldPluginManager $mappingFieldPluginManager, RestClientInterface $client, SalesforceMappableEntityTypesInterface $mappableEntityTypes, EntityTypeBundleInfoInterface $bundleInfo) {
$this->mappingFieldPluginManager = $mappingFieldPluginManager;
$this->client = $client;
$this->mappableEntityTypes = $mappableEntityTypes;
$this->bundleInfo = $bundleInfo;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.salesforce_mapping_field'), $container
->get('salesforce.client'), $container
->get('salesforce_mapping.mappable_entity_types'), $container
->get('entity_type.bundle.info'));
}
protected function ensureConnection($method = 'objects', $arg = [
[],
TRUE,
]) {
$message = '';
if ($this->client
->isInit()) {
try {
call_user_func_array([
$this->client,
$method,
], $arg);
return TRUE;
} catch (\Exception $e) {
$message = $e
->getMessage() ?: get_class($e);
}
}
$href = new Url('salesforce.auth_config');
$this
->messenger()
->addError($this
->t('Error when connecting to Salesforce. Please <a href="@href">check your credentials</a> and try again: %message', [
'@href' => $href
->toString(),
'%message' => $message,
]), 'error');
return FALSE;
}
public function save(array $form, FormStateInterface $form_state) {
if (!$this->entity
->save()) {
$this
->messenger()
->addError($this
->t('An error occurred while trying to save the mapping.'));
return;
}
$this
->messenger()
->addStatus($this
->t('The mapping has been successfully saved.'));
}
protected function getSalesforceObject($salesforce_object_type = '') {
if (empty($salesforce_object_type)) {
$salesforce_object_type = $this->entity
->get('salesforce_object_type');
}
if (empty($salesforce_object_type)) {
throw new \Exception('Salesforce object type not set.');
}
return $this->client
->objectDescribe($salesforce_object_type);
}
protected function getSalesforceObjectTypeOptions() {
$sfobject_options = [];
$config = $this
->config('salesforce.settings');
$filter = $config
->get('show_all_objects') ? [] : [
'updateable' => TRUE,
'triggerable' => TRUE,
];
$sfobjects = $this->client
->objects($filter);
foreach ($sfobjects as $object) {
$sfobject_options[$object['name']] = $object['label'] . ' (' . $object['name'] . ')';
}
asort($sfobject_options);
return $sfobject_options;
}
}