You are here

public function LearningPathManagerController::ajaxFormEntityFormSubmit in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Controller/LearningPathManagerController.php \Drupal\opigno_learning_path\Controller\LearningPathManagerController::ajaxFormEntityFormSubmit()

Submit added via the function opigno_learning_path_form_alter().

See also

opigno_learning_path_form_alter()

File

src/Controller/LearningPathManagerController.php, line 126

Class

LearningPathManagerController
Controller for all the actions of the Learning Path manager app.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function ajaxFormEntityFormSubmit($form, FormState &$form_state) {

  // Gets back the content type and learning path id.
  $build_info = $form_state
    ->getBuildInfo();
  foreach ($build_info['args'] as $arg_key => $arg_value) {
    if ($arg_key === 'learning_path_info') {
      $lp_id = $arg_value['learning_path_id'];
      $lp_content_type_id = $arg_value['lp_content_type'];
      break;
    }
  }

  // If one information missing, return an error.
  if (!isset($lp_id) || !isset($lp_content_type_id)) {

    // TODO: Add an error message here.
    return;
  }

  // Get the newly or edited entity.
  $entity = $build_info['callback_object']
    ->getEntity();

  // Clear user input.
  $input = $form_state
    ->getUserInput();

  // We should not clear the system items from the user input.
  $clean_keys = $form_state
    ->getCleanValueKeys();
  $clean_keys[] = 'ajax_page_state';
  foreach ($input as $key => $item) {
    if (!in_array($key, $clean_keys) && substr($key, 0, 1) !== '_') {
      unset($input[$key]);
    }
  }

  // Store new entity for display in the AJAX callback.
  $input['entity'] = $entity;
  $form_state
    ->setUserInput($input);

  // Rebuild the form state values.
  $form_state
    ->setRebuild();
  $form_state
    ->setStorage([]);
}