You are here

protected function DeveloperAppCreateForm::alterFormBeforeApiProductElement in Apigee Edge 8

Allows to alter the form before API products gets added.

Parameters

array $form: Form render array.

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

Overrides AppCreateForm::alterFormBeforeApiProductElement

File

src/Entity/Form/DeveloperAppCreateForm.php, line 89

Class

DeveloperAppCreateForm
General form handler for the developer app create.

Namespace

Drupal\apigee_edge\Entity\Form

Code

protected function alterFormBeforeApiProductElement(array &$form, FormStateInterface $form_state) : void {

  // Do not reload a developer ids and users when AJAX refreshes the form.
  $developer_options = $form_state
    ->get('developer_options');
  if ($developer_options === NULL) {

    // It is faster to collect existing developer emails like this
    // from Apigee Edge.
    $developer_emails = $this->developerController
      ->getEntityIds();
    $developer_options = array_reduce($this->entityTypeManager
      ->getStorage('user')
      ->loadByProperties([
      'mail' => $developer_emails,
    ]), function ($carry, UserInterface $item) {
      $carry[$item
        ->getEmail()] = $item
        ->label();
      return $carry;
    }, []);
    reset($developer_options);
    $form_state
      ->set('developer_options', $developer_options);
  }

  // Override the owner field to be a select list with all developers from
  // Apigee Edge.
  $form['owner'] = [
    '#title' => $this
      ->t('Owner'),
    '#type' => 'select',
    '#weight' => $form['owner']['#weight'],
    '#default_value' => $form_state
      ->get('owner') ?? key($developer_options),
    '#options' => $developer_options,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::updateApiProductList',
    ],
  ];
}