You are here

public function CourseOutlineForm::submitForm in Course 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/CourseOutlineForm.php \Drupal\course\Form\CourseOutlineForm::submitForm()
  2. 3.x src/Form/CourseOutlineForm.php \Drupal\course\Form\CourseOutlineForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/CourseOutlineForm.php, line 169

Class

CourseOutlineForm

Namespace

Drupal\course\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $course = $form_state
    ->getBuildInfo()['args'][0];

  // Get form state values for object elements on the course outline overview:
  // - An associative array of course objects, keyed by ID. The ID for already
  //   saved objects is {course_object}.coid, but for AHAH created objects the
  //   key is a generated unique ID until save.
  //   - coid: The key loaded from the database. If empty, the object is new.
  //   - module: The implementing module name (course_quiz etc).
  //   - object_type: The course object key as defined by
  //     hook_course_handlers().
  $objects = $form_state
    ->getValue('course_outline');

  // Sort by weight so we can renumber.
  uasort($objects, 'static::sortCourseOutline');
  foreach ($objects as $object_key => $object) {

    // Load object from session.

    /* @var $courseObject \Drupal\course\Entity\CourseObject */
    if (!($courseObject = _course_get_course_object_by_uniqid($object_key))) {
      $courseObject = \Drupal\course\Entity\CourseObject::load($object_key);
    }
    if ($courseObject
      ->id()) {

      // This isn't new.
      $courseObject
        ->enforceIsNew(FALSE);
    }

    // Renumber weights to the way draggable table would do it in case of no JS.
    $courseObject
      ->setOption('weight', $object['weight']);
    if ($courseObject
      ->getOption('delete')) {

      // Delete the course object.
      if ($courseObject
        ->getOption('delete_instance')) {

        // Also delete the course object's content.
        $courseObject
          ->deleteInstance();
      }
      $courseObject
        ->delete();
    }
    else {
      $courseObject
        ->save();
    }
  }

  // Clear the editing session.
  unset($_SESSION['course'][$course
    ->id()]['editing']);
  \Drupal::messenger()
    ->addStatus(t('Updated course.'));
  $form_state
    ->setRedirect('course.outline', [
    'course' => $course
      ->id(),
  ]);
}