You are here

public function AppEditForm::form in Apigee Edge 8

Gets the actual form array to be built.

Overrides AppForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

1 call to AppEditForm::form()
TeamAppEditForm::form in modules/apigee_edge_teams/src/Entity/Form/TeamAppEditForm.php
Gets the actual form array to be built.
1 method overrides AppEditForm::form()
TeamAppEditForm::form in modules/apigee_edge_teams/src/Entity/Form/TeamAppEditForm.php
Gets the actual form array to be built.

File

src/Entity/Form/AppEditForm.php, line 73

Class

AppEditForm
Base entity form for developer- and team (company) app edit forms.

Namespace

Drupal\apigee_edge\Entity\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['#cache']['contexts'][] = 'user.permissions';

  /** @var \Drupal\apigee_edge\Entity\AppInterface $app */
  $app = $this->entity;
  $app_settings = $this
    ->config('apigee_edge.common_app_settings');
  $is_multiple_selection = $app_settings
    ->get('multiple_products');
  $api_product_def = $this->entityTypeManager
    ->getDefinition('api_product');

  // Do not allow to change the (machine) name of the app.
  $form['name'] = [
    '#type' => 'value',
    '#value' => $app
      ->getName(),
  ];

  // Do not allow to change the owner of the app.
  $form['owner']['#access'] = FALSE;

  // If app's display name is empty then fallback to app name as default
  // value just like Apigee Edge Management UI does.
  if ($form['displayName']['widget'][0]['value']['#default_value'] === NULL) {
    $form['displayName']['widget'][0]['value']['#default_value'] = $app
      ->getName();
  }

  // If app's callback URL field is visible on the form then set its value
  // to the callback url property's value always, because it could happen that
  // its value is empty if the saved value is not a valid URL.
  // (Apigee Edge Management API does not validate the value of the
  // callback URL, but Drupal does.)
  if (isset($form['callbackUrl'])) {
    $form['callbackUrl']['widget'][0]['value']['#default_value'] = $app
      ->getCallbackUrl();
  }

  // If "Let user select the product(s)" is enabled.
  if ($app_settings
    ->get('user_select')) {
    $available_products_by_user = $this
      ->apiProductList($form, $form_state);
    $form['credential'] = [
      '#type' => 'container',
      '#weight' => 100,
    ];
    foreach ($app
      ->getCredentials() as $credential) {
      $credential_status_element = [
        '#type' => 'status_property',
        '#value' => Xss::filter($credential
          ->getStatus()),
        '#indicator_status' => $credential
          ->getStatus() === AppCredentialInterface::STATUS_APPROVED ? StatusPropertyElement::INDICATOR_STATUS_OK : StatusPropertyElement::INDICATOR_STATUS_ERROR,
      ];
      $rendered_credential_status = $this->render
        ->render($credential_status_element);
      $form['credential'][$credential
        ->getConsumerKey()] = [
        '#type' => 'fieldset',
        '#title' => $rendered_credential_status . $this
          ->t('Credential'),
        '#collapsible' => FALSE,
      ];

      // List of API product (ids/names) that the credential currently
      // contains.
      $credential_currently_assigned_product_ids = [];
      foreach ($credential
        ->getApiProducts() as $product) {
        $credential_currently_assigned_product_ids[] = $product
          ->getApiproduct();
      }

      // $available_products_by_user ensures that only those API products
      // are visible in this list that the user can access.
      // But we have to add this app credential's currently assigned API
      // products to the list as well.
      $credential_product_options = array_map(function (ApiProductInterface $product) {
        return $product
          ->label();
      }, $available_products_by_user + $this->entityTypeManager
        ->getStorage('api_product')
        ->loadMultiple($credential_currently_assigned_product_ids));
      $form['credential'][$credential
        ->getConsumerKey()]['api_products'] = [
        '#title' => $api_product_def
          ->getPluralLabel(),
        '#required' => TRUE,
        '#options' => $credential_product_options,
        '#disabled' => !$this
          ->canEditApiProducts(),
      ];
      if ($is_multiple_selection) {
        $form['credential'][$credential
          ->getConsumerKey()]['api_products']['#default_value'] = $credential_currently_assigned_product_ids;
      }
      else {
        if (count($credential_currently_assigned_product_ids) > 1) {
          $this
            ->messenger()
            ->addWarning($this
            ->t('@apps now require selection of a single @api_product; multiple @api_product selection is no longer supported. Confirm your @api_product selection below.', [
            '@apps' => $this
              ->appEntityDefinition()
              ->getPluralLabel(),
            '@api_product' => $api_product_def
              ->getSingularLabel(),
          ]));
        }
        $form['credential'][$credential
          ->getConsumerKey()]['api_products']['#default_value'] = reset($credential_currently_assigned_product_ids) ?: NULL;
      }
      if ($app_settings
        ->get('display_as_select')) {
        $form['credential'][$credential
          ->getConsumerKey()]['api_products']['#type'] = 'select';
        $form['credential'][$credential
          ->getConsumerKey()]['api_products']['#multiple'] = $is_multiple_selection;
        $form['credential'][$credential
          ->getConsumerKey()]['api_products']['#empty_value'] = '';
      }
      else {
        if ($is_multiple_selection) {
          $form['credential'][$credential
            ->getConsumerKey()]['api_products']['#type'] = 'checkboxes';
          $form['credential'][$credential
            ->getConsumerKey()]['api_products']['#options'] = $credential_product_options;
        }
        else {
          $form['credential'][$credential
            ->getConsumerKey()]['api_products']['#type'] = 'radios';
          $form['credential'][$credential
            ->getConsumerKey()]['api_products']['#options'] = $credential_product_options;
        }
      }
    }
  }
  return $form;
}