You are here

public function AppSettingsForm::buildForm in Apigee Edge 8

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/AppSettingsForm.php, line 98

Class

AppSettingsForm
Configuration form builder for general app settings.

Namespace

Drupal\apigee_edge\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $common_app_settings = $this
    ->config('apigee_edge.common_app_settings');

  // Someone has overridden the default setting.
  if (!$common_app_settings
    ->get('multiple_products')) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Access to multiple API products will be retained until an app is edited and the developer is prompted to confirm a single API product selection.'));
  }

  /** @var string[] $default_products */
  $default_products = $common_app_settings
    ->get('default_products') ?: [];
  $product_list = [];
  try {

    /** @var \Drupal\apigee_edge\Entity\ApiProduct $product */
    foreach ($this->entityTypeManager
      ->getStorage('api_product')
      ->loadMultiple() as $product) {
      $product_list[$product
        ->id()] = $product
        ->label();
    }
  } catch (EntityStorageException $e) {

    // Apigee Edge credentials are missing/incorrect or something else went
    // wrong. Do not redirect the user to the error page.
    $form['actions']['submit']['#disabled'] = TRUE;
    $this
      ->messenger()
      ->addError($this
      ->t('Unable to retrieve API product list from Apigee Edge. Please ensure that <a href=":link">Apigee Edge connection settings</a> are correct.', [
      ':link' => Url::fromRoute('apigee_edge.settings')
        ->toString(),
    ]));
    return $form;
  }
  $form['api_product'] = [
    '#id' => 'api_product',
    '#type' => 'details',
    '#title' => $this
      ->t('API product association settings'),
    '#open' => TRUE,
  ];
  $form['api_product']['multiple_products'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow multiple products'),
    '#description' => $this
      ->t('Allow users to select multiple products.'),
    '#default_value' => $common_app_settings
      ->get('multiple_products'),
  ];
  $form['api_product']['display_as_select'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display the API Product widget as a select box (instead of checkboxes/radios)'),
    '#default_value' => $common_app_settings
      ->get('display_as_select'),
  ];
  $form['api_product']['user_select'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Let user select the product(s)'),
    '#default_value' => $common_app_settings
      ->get('user_select'),
    '#ajax' => [
      'callback' => '::apiProductListCallback',
      'wrapper' => 'default-api-product-multiple',
      'progress' => [
        'type' => 'throbber',
        'message' => '',
      ],
    ],
  ];

  // It's necessary to add a wrapper because if the ID is added to the
  // checkboxes form element, then that will not be properly rendered
  // (the label gets duplicated).
  $form['api_product']['default_api_product_multiple_container'] = [
    '#type' => 'container',
    '#id' => 'default-api-product-multiple',
  ];
  $form['api_product']['default_api_product_multiple_container']['default_api_product_multiple'] = [
    '#type' => 'checkboxes',
    '#title' => $common_app_settings
      ->get('multiple_products') ? $this
      ->t('Default API products') : $this
      ->t('Default API product'),
    '#options' => $product_list,
    '#default_value' => $default_products,
    '#required' => $form_state
      ->getValue('user_select') === NULL ? !(bool) $common_app_settings
      ->get('user_select') : !(bool) $form_state
      ->getValue('user_select'),
  ];
  return parent::buildForm($form, $form_state);
}