You are here

public static function OpignoGroupManagerController::ajaxFormEntityFormSubmit in Opigno group manager 3.x

Same name and namespace in other branches
  1. 8 src/Controller/OpignoGroupManagerController.php \Drupal\opigno_group_manager\Controller\OpignoGroupManagerController::ajaxFormEntityFormSubmit()

Submit handler added by opigno_learning_path_form_alter().

See also

opigno_learning_path_form_alter()

File

src/Controller/OpignoGroupManagerController.php, line 277

Class

OpignoGroupManagerController
Controller for all the actions of the Opigno group manager app.

Namespace

Drupal\opigno_group_manager\Controller

Code

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

  // Gets back the content type and learning path id.
  $build_info = $form_state
    ->getBuildInfo();
  foreach ($build_info['args'][0] as $arg_key => $arg_value) {
    if ($arg_key === 'opigno_group_info') {
      $lp_id = $arg_value['group_id'];
      $lp_content_type_id = $arg_value['opigno_group_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();
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('opigno_skills_system') && $lp_content_type_id == 'ContentTypeModule') {
    $managed_contents = OpignoGroupManagedContent::loadByProperties([
      'group_content_type_id' => 'ContentTypeModule',
      'group_id' => $lp_id,
      'entity_id' => $entity
        ->id(),
    ]);
    if (!empty($managed_contents)) {
      $content = reset($managed_contents);
      $content
        ->setSkillSystem($entity
        ->getSkillsActive());
      if ($entity
        ->getSkillsActive()) {
        $content
          ->setMandatory(0);
      }
      $content
        ->save();
    }
  }

  // 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([]);
}