You are here

public function Square::buildConfigurationForm in Commerce Square Connect 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides OnsitePaymentGatewayBase::buildConfigurationForm

File

src/Plugin/Commerce/PaymentGateway/Square.php, line 99

Class

Square
Provides the Square payment gateway.

Namespace

Drupal\commerce_square\Plugin\Commerce\PaymentGateway

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  if (empty($this->connect
    ->getAppId('sandbox')) && empty($this->connect
    ->getAccessToken('sandbox'))) {
    $this
      ->messenger()
      ->addError($this
      ->t('Square has not been configured, please go to :link', [
      ':link' => Link::fromTextAndUrl($this
        ->t('the settings form'), Url::fromRoute('commerce_square.settings')),
    ]));
  }
  foreach (array_keys($this
    ->getSupportedModes()) as $mode) {
    $form[$mode] = [
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
      '#title' => $this
        ->t('@mode location', [
        '@mode' => $this->pluginDefinition['modes'][$mode],
      ]),
    ];
    $form[$mode][$mode . '_location_id'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Location'),
      '#description' => $this
        ->t('The location for the transactions.'),
      '#default_value' => $this->configuration[$mode . '_location_id'],
      '#required' => TRUE,
    ];
    $api_mode = $mode === 'test' ? 'sandbox' : 'production';
    $client = $this->connect
      ->getClient($api_mode);
    $location_api = new LocationsApi($client);
    try {
      $locations = $location_api
        ->listLocations();
      $location_options = $locations
        ->getLocations();
      $options = [];
      foreach ($location_options as $location_option) {
        $options[$location_option
          ->getId()] = $location_option
          ->getName();
      }
      $form[$mode][$mode . '_location_id']['#options'] = $options;
    } catch (\Exception $e) {
      $form[$mode][$mode . '_location_id']['#disabled'] = TRUE;
      $form[$mode][$mode . '_location_id']['#options'] = [
        '_none' => 'Not configured',
      ];
    }
  }
  return $form;
}