You are here

protected function TeamAppCreateForm::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

modules/apigee_edge_teams/src/Entity/Form/TeamAppCreateForm.php, line 36

Class

TeamAppCreateForm
General form handler for the team app create.

Namespace

Drupal\apigee_edge_teams\Entity\Form

Code

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

  // Do not recalculate team options when AJAX refreshes the form.
  $team_options = $form_state
    ->get('team_options');
  if ($team_options === NULL) {
    $team_options = array_map(function (TeamInterface $team) {
      return $team
        ->label();
    }, $this->entityTypeManager
      ->getStorage('team')
      ->loadMultiple());
    reset($team_options);
    $form_state
      ->set('team_options', $team_options);
  }

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