You are here

final public function AppCreateForm::validateApiProductSelection in Apigee Edge 8

Element validate callback for the API product list.

Ensures that even if "Let user select the product(s)" is disabled the submitted form contains at least one valid API product. (It could happen that someone changed this configuration from CMI but forgot to select at least one "Default API product" or the selected default API product does not exist anymore.)

Parameters

array $element: Element to validate.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

array $complete_form: The complete form.

File

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

Class

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

Namespace

Drupal\apigee_edge\Entity\Form

Code

public final function validateApiProductSelection(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // Field is required so we only need to validate this if the user does not
  // have access to the form element.
  if (!$element['#access']) {
    $selected_products = array_values(array_filter((array) $form_state
      ->getValue($element['#parents'])));

    // It is faster to collect existing API product names from Apigee Edge
    // like this.
    $existing_products = $this->apiProductController
      ->getEntityIds();
    $sanitized_product_list = array_intersect($selected_products, $existing_products);
    if ($sanitized_product_list != $selected_products) {

      // Something went wrong...
      $form_state
        ->setError($complete_form, $this
        ->t('@app creation is temporarily disabled. Please contact with support.', [
        '@app' => $this
          ->appEntityDefinition()
          ->getSingularLabel(),
      ]));
      $this
        ->logger('apigee_edge')
        ->critical('Invalid configuration detected! "Let user select the product(s)" is disabled but the submitted app creation form did contain at least one invalid API product. App creation process has been aborted. Please verify the configuration.<br>API product ids in input: <pre>@input</pre> API Product ids on Apigee Edge: <pre>@existing</pre>', [
        'link' => Link::fromTextAndUrl($this
          ->t('configuration'), Url::fromRoute('apigee_edge.settings.general_app'))
          ->toString(),
        '@input' => print_r($selected_products, TRUE),
        '@existing' => print_r($existing_products, TRUE),
      ]);
    }
  }
}