You are here

function CourseOutlineForm::addObject in Course 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/CourseOutlineForm.php \Drupal\course\Form\CourseOutlineForm::addObject()
  2. 3.x src/Form/CourseOutlineForm.php \Drupal\course\Form\CourseOutlineForm::addObject()
1 call to CourseOutlineForm::addObject()
CourseOutlineForm::buildForm in src/Form/CourseOutlineForm.php
Form constructor.

File

src/Form/CourseOutlineForm.php, line 222

Class

CourseOutlineForm

Namespace

Drupal\course\Form

Code

function addObject(array &$form, FormStateInterface $form_state) {

  // Check if "Add object" was clicked.
  if ($form_state
    ->getTriggeringElement()['#value'] == 'Add object' && !empty($form_state
    ->getValues()['more']['object_type'])) {
    $course = $form_state
      ->getBuildInfo()['args'][0];

    // Ensure that we cached the course.
    course_editing_start($course);

    // Create a new course object in the session, and let the rest of the form
    // builder handle it.
    $obj_uniqid = uniqid('course_object_');
    $_SESSION['course'][$course
      ->id()]['editing'][$obj_uniqid] = array();

    // Populate temporary course object, save it in the session.
    $new = array();
    $new['weight'] = 0;

    // Get highest weight and add to it.
    if (isset($form_state
      ->getValues()['course_outline']['objects'])) {
      foreach ($form_state
        ->getValues()['course_outline']['objects'] as $key => $object) {
        if ($object['weight'] >= $new['weight']) {
          $new['weight'] = $object['weight'] + 1;
        }
      }
    }
    $new['cid'] = $course
      ->id();
    $new['coid'] = $obj_uniqid;
    $new['object_type'] = $form_state
      ->getValues()['more']['object_type'];
    $_SESSION['course'][$course
      ->id()]['editing'][$obj_uniqid] = $new;
    $form_state
      ->setValue('last', $obj_uniqid);
  }
}