You are here

public function SalesforceAuthForm::form in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 5.0.x src/Form/SalesforceAuthForm.php \Drupal\salesforce\Form\SalesforceAuthForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/SalesforceAuthForm.php, line 23

Class

SalesforceAuthForm
Entity form for salesforce_auth.

Namespace

Drupal\salesforce\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $auth = $this->entity;
  if (empty($auth
    ->getPluginsAsOptions())) {
    $this
      ->messenger()
      ->addError('No auth provider plugins found. Please enable an auth provider module, e.g. salesforce_jwt, before adding an auth config.');
    $form['#access'] = FALSE;
    return $form;
  }
  $form_state
    ->setBuildInfo($form_state
    ->getBuildInfo() + [
    'auth_config' => $this
      ->config($auth
      ->getConfigDependencyName()),
  ]);
  $form['label'] = [
    '#title' => $this
      ->t('Label'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('User-facing label for this project, e.g. "OAuth Full Sandbox"'),
    '#default_value' => $auth
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $auth
      ->id(),
    '#maxlength' => 32,
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
      'source' => [
        'label',
      ],
    ],
    '#required' => TRUE,
  ];

  // This is the element that contains all of the dynamic parts of the form.
  $form['settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Settings'),
    '#open' => TRUE,
  ];
  $form['settings']['provider'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Auth provider'),
    '#options' => $auth
      ->getPluginsAsOptions(),
    '#required' => TRUE,
    '#default_value' => $auth
      ->getPluginId(),
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxUpdateSettings',
      ],
      'event' => 'change',
      'wrapper' => 'auth-settings',
    ],
  ];
  $default = [
    '#type' => 'container',
    '#title' => $this
      ->t('Auth provider settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
    '#prefix' => '<div id="auth-settings">',
    '#suffix' => '</div>',
  ];
  $form['settings']['provider_settings'] = $default;
  if ($auth
    ->getPlugin() && !$form_state
    ->isRebuilding()) {
    $form['settings']['provider_settings'] += $auth
      ->getPlugin()
      ->buildConfigurationForm([], $form_state);
  }
  elseif ($form_state
    ->getValue('provider')) {
    $plugin = $this->entity
      ->authManager()
      ->createInstance($form_state
      ->getValue('provider'));
    $form['settings']['provider_settings'] += $plugin
      ->buildConfigurationForm([], $form_state);
  }
  elseif ($form_state
    ->getUserInput()) {
    $input = $form_state
      ->getUserInput();
    if (!empty($input['provider'])) {
      $plugin = $this->entity
        ->authManager()
        ->createInstance($input['provider']);
      $form['settings']['provider_settings'] += $plugin
        ->buildConfigurationForm([], $form_state);
    }
  }
  $form['save_default'] = [
    '#type' => 'checkbox',
    '#title' => 'Save and set default',
    '#default_value' => $this->entity
      ->isNew() || $this->entity
      ->authManager()
      ->getProvider() && $this->entity
      ->authManager()
      ->getProvider()
      ->id() == $this->entity
      ->id(),
  ];
  return parent::form($form, $form_state);
}