You are here

final protected function AppCreateForm::apiProductsFormElement in Apigee Edge 8

Returns the API Products form element element.

Form and form state is only passed to be able filter API products that should be displayed.

Parameters

array $form: Form render array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

Return value

array The API product render element

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

See also

apiProductList()

1 call to AppCreateForm::apiProductsFormElement()
AppCreateForm::form in src/Entity/Form/AppCreateForm.php
Gets the actual form array to be built.

File

src/Entity/Form/AppCreateForm.php, line 120

Class

AppCreateForm
Base entity form for developer- and team (company) app create forms.

Namespace

Drupal\apigee_edge\Entity\Form

Code

protected final function apiProductsFormElement(array $form, FormStateInterface $form_state) : array {
  $app_settings = $this
    ->config('apigee_edge.common_app_settings');
  $user_select = (bool) $app_settings
    ->get('user_select');
  $api_products_options = array_map(function (ApiProductInterface $product) {
    return $product
      ->label();
  }, $this
    ->apiProductList($form, $form_state));
  $multiple = $app_settings
    ->get('multiple_products');
  $default_products = $app_settings
    ->get('default_products') ?: [];
  $element = [
    '#title' => $this->entityTypeManager
      ->getDefinition('api_product')
      ->getPluralLabel(),
    '#required' => TRUE,
    '#options' => $api_products_options,
    '#access' => $user_select,
    '#weight' => 100,
    '#default_value' => $multiple ? $default_products : (string) reset($default_products),
    '#element_validate' => [
      '::validateApiProductSelection',
    ],
  ];
  if ($app_settings
    ->get('display_as_select')) {
    $element['#type'] = 'select';
    $element['#multiple'] = $multiple;
    $element['#empty_value'] = '';
  }
  else {
    $element['#type'] = $multiple ? 'checkboxes' : 'radios';
    if (!$multiple) {
      $element['#options'] = [
        '' => $this
          ->t('N/A'),
      ] + $element['#options'];
    }
  }
  return $element;
}