You are here

public static function OpignoModuleManagerController::ajaxFormEntityFormSubmit in Opigno module 8

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

Submit handler added in the form by opigno_module_form_alter().

See also

opigno_module_form_alter()

File

src/Controller/OpignoModuleManagerController.php, line 200

Class

OpignoModuleManagerController
Controller for all the actions of the Opigno module manager.

Namespace

Drupal\opigno_module\Controller

Code

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

  // Gets back the content type and module id.
  $build_info = $form_state
    ->getBuildInfo();
  $params = \Drupal::routeMatch()
    ->getParameters();
  $module = $params
    ->get('opigno_module');
  $type_id = $params
    ->get('type');
  $item_id = $params
    ->get('item');

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

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

  // Get the newly or edited entity.

  /** @var \Drupal\Core\Entity\EntityInterface $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([]);

  // Assign activity to module if entity is new.
  if (!isset($item_id)) {

    /** @var \Drupal\opigno_module\Controller\OpignoModuleController $opigno_module_controller */
    $opigno_module_controller = \Drupal::service('opigno_module.opigno_module');
    $opigno_module_controller
      ->activitiesToModule([
      $entity,
    ], $module);
  }
}