You are here

public static function ExternalPackageController::ajaxFormExternalPackageCallback in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Controller/ExternalPackageController.php \Drupal\opigno_module\Controller\ExternalPackageController::ajaxFormExternalPackageCallback()

External package form ajax callback.

File

src/Controller/ExternalPackageController.php, line 26

Class

ExternalPackageController
Class ActivitiesBrowserController.

Namespace

Drupal\opigno_module\Controller

Code

public static function ajaxFormExternalPackageCallback(&$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();

  // If any errors - return form.
  if ($form_state
    ->hasAnyErrors()) {
    if ($errors = $form_state
      ->getErrors()) {
      $errors_value = [];
      foreach ($errors as $error) {
        $errors_value[] = [
          '#type' => 'html_tag',
          '#tag' => 'li',
          '#value' => $error,
        ];
      }
      $form['errors'] = [
        '#type' => 'container',
        '#weight' => 99,
        '#attributes' => [
          'class' => [
            'alert',
            'alert-danger',
            'opigno-alert-danger',
          ],
          'role' => [
            'alert',
          ],
          'aria-label' => [
            'Error message',
          ],
        ],
        'content' => [
          '#type' => 'html_tag',
          '#tag' => 'ul',
          'content' => $errors_value,
        ],
      ];

      // Delete errors to avoid duplication.
      \Drupal::messenger()
        ->deleteByType(MessengerInterface::TYPE_ERROR);
    }

    // Return form with ajax response.
    $response
      ->addCommand(new ReplaceCommand('.ajax-form-entity-external-package', $form));
    return $response;
  }
  if ($entity = $form_state
    ->getUserInput()['activity']) {
    $item = [];
    $item['id'] = $entity
      ->id();
    $item['name'] = $entity
      ->getName();
    $command = new SettingsCommand([
      'formValues' => $item,
      'messages' => \Drupal::messenger()
        ->all(),
    ], TRUE);
  }
  else {
    $command = new SettingsCommand([
      'messages' => \Drupal::messenger()
        ->all(),
    ], TRUE);
  }
  \Drupal::messenger()
    ->deleteAll();
  $response
    ->addCommand($command);
  return $response;
}