SalesforceMappingFormBase.php in Salesforce Suite 8.3
File
modules/salesforce_mapping/src/Form/SalesforceMappingFormBase.php
View source
<?php
namespace Drupal\salesforce_mapping\Form;
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;
public function __construct(SalesforceMappingFieldPluginManager $mappingFieldPluginManager, RestClientInterface $client, SalesforceMappableEntityTypesInterface $mappableEntityTypes) {
$this->mappingFieldPluginManager = $mappingFieldPluginManager;
$this->client = $client;
$this->mappableEntityTypes = $mappableEntityTypes;
}
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'));
}
protected function ensureConnection($method = 'objects', $arg = []) {
try {
$this->client
->{$method}($arg);
} catch (\Exception $e) {
$href = new Url('salesforce.admin_config_salesforce');
$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' => $e
->getMessage() ?: get_class($e),
]), 'error');
return FALSE;
}
return TRUE;
}
public function save(array $form, FormStateInterface $form_state) {
if (!$this->entity
->save()) {
drupal_set_message($this
->t('An error occurred while trying to save the mapping.'));
return;
}
drupal_set_message($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;
}
}