public function AuthorizeForm::buildForm in Salesforce Suite 8.3
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ AuthorizeForm.php, line 96
Class
- AuthorizeForm
- Creates authorization form for Salesforce.
Namespace
Drupal\salesforce\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('salesforce.settings');
$encrypted = is_subclass_of($this->client, EncryptedRestClientInterface::class);
$url = new Url('salesforce.oauth_callback', [], [
'absolute' => TRUE,
]);
drupal_set_message($this
->t('Callback URL: :url', [
':url' => str_replace('http:', 'https:', $url
->toString()),
]));
$form['creds'] = [
'#title' => $this
->t('API / OAuth Connection Settings'),
'#type' => 'details',
'#open' => TRUE,
'#description' => $this
->t('Authorize this website to communicate with Salesforce by entering the consumer key and secret from a remote application. Submitting the form will redirect you to Salesforce where you will be asked to grant access.'),
];
$form['creds']['consumer_key'] = [
'#title' => $this
->t('Salesforce consumer key'),
'#type' => 'textfield',
'#description' => $this
->t('Consumer key of the Salesforce remote application you want to grant access to'),
'#required' => TRUE,
'#default_value' => $encrypted ? $this->client
->decrypt($config
->get('consumer_key')) : $config
->get('consumer_key'),
];
$form['creds']['consumer_secret'] = [
'#title' => $this
->t('Salesforce consumer secret'),
'#type' => 'textfield',
'#description' => $this
->t('Consumer secret of the Salesforce remote application you want to grant access to'),
'#required' => TRUE,
'#default_value' => $encrypted ? $this->client
->decrypt($config
->get('consumer_secret')) : $config
->get('consumer_secret'),
];
$form['creds']['login_url'] = [
'#title' => $this
->t('Login URL'),
'#type' => 'textfield',
'#default_value' => empty($config
->get('login_url')) ? 'https://login.salesforce.com' : $config
->get('login_url'),
'#description' => $this
->t('Enter a login URL, either https://login.salesforce.com or https://test.salesforce.com.'),
'#required' => TRUE,
];
// If fully configured, attempt to connect to Salesforce and return a list
// of resources.
if ($this->client
->isAuthorized()) {
$form['creds']['#open'] = FALSE;
$form['creds']['#description'] = $this
->t('Your Salesforce salesforce instance is currently authorized. Enter credentials here only to change credentials.');
try {
$resources = $this->client
->listResources();
foreach ($resources->resources as $key => $path) {
$items[] = $key . ': ' . $path;
}
if (!empty($items)) {
$form['resources'] = [
'#title' => $this
->t('Your Salesforce instance is authorized and has access to the following resources:'),
'#items' => $items,
'#theme' => 'item_list',
];
}
} catch (\Exception $e) {
// Do not allow any exceptions to interfere with displaying this page.
drupal_set_message($e
->getMessage(), 'warning');
$this->eventDispatcher
->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));
}
}
elseif (!$form_state
->getUserInput()) {
// Don't set this message if the form was submitted.
drupal_set_message(t('Salesforce needs to be authorized to connect to this website.'), 'error');
}
$form = parent::buildForm($form, $form_state);
$form['creds']['actions'] = $form['actions'];
unset($form['actions']);
return $form;
}