function CourseOutlineForm::addObject in Course 3.x
Same name and namespace in other branches
- 8.3 src/Form/CourseOutlineForm.php \Drupal\course\Form\CourseOutlineForm::addObject()
- 8.2 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 232
Class
Namespace
Drupal\course\FormCode
function addObject(array &$form, FormStateInterface $form_state) {
// Check if "Add object" was clicked.
if ($form_state
->getTriggeringElement() && $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(
'weight' => 0,
);
// Populate temporary course object, save it in the session.
$new = array();
$new['weight'] = max(array_column($_SESSION['course'][$course
->id()]['editing'], 'weight')) + 1;
// 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);
}
}