public function CourseOutlineForm::buildForm in Course 8.2
Same name and namespace in other branches
- 8.3 src/Form/CourseOutlineForm.php \Drupal\course\Form\CourseOutlineForm::buildForm()
- 3.x src/Form/CourseOutlineForm.php \Drupal\course\Form\CourseOutlineForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ CourseOutlineForm.php, line 31
Class
Namespace
Drupal\course\FormCode
public function buildForm(array $form, FormStateInterface $form_state, $course = NULL) {
// Wrapper for objects and more button.
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
// Shortcut to the course outline table.
$cform =& $form['course_outline'];
$cform['#prefix'] = '<div class="clear-block" id="course-outline-wrapper">';
$cform['#suffix'] = '</div>';
$cform['#attached']['library'][] = 'course/admin-css';
// Add object if button was pressed.
$this
->addObject($form, $form_state);
// Grab initial list of objects from DB or session.
if (!empty($_SESSION['course'][$course
->id()]['editing'])) {
$objects = $_SESSION['course'][$course
->id()]['editing'];
}
else {
if ($objects = $course
->getObjects()) {
// Great.
}
else {
$objects = array();
}
}
// Sort list of objects we pulled from session or DB by weight for proper
// display.
uasort($objects, 'static::sortCourseOutline');
$cform['#title'] = t('Course objects');
//$form['#theme'] = 'course_outline_overview_form';
if (empty($_POST) && !empty($_SESSION['course'][$course
->id()]['editing'])) {
\Drupal::messenger()
->addWarning(t('Changes to this course have not yet been saved.'));
}
$handlers = course_get_handlers('object');
// Wrapper for just the objects.
$cform['#tree'] = TRUE;
$cform['#type'] = 'table';
$cform['#id'] = 'edit-course-outline';
$cform['#empty'] = $this
->t('No objects. Add an object!');
$cform['#header'] = [
$this
->t('Description'),
$this
->t('Object'),
$this
->t('Actions'),
$this
->t('Weight'),
];
$object_counts = array();
if (count($objects)) {
foreach (array_keys($objects) as $uniqid) {
if ($courseObject = course_get_course_object_by_id($uniqid)) {
$rform = $this
->formObject($courseObject);
// Keep track of how many of each course object we have.
// @kludge probably some simpler way to do this effectively
if (!isset($object_counts[$courseObject
->getComponent()])) {
$object_counts[$courseObject
->getComponent()] = 1;
}
else {
$object_counts[$courseObject
->getComponent()]++;
}
$cform[$uniqid] = $rform;
}
}
}
// Add object button and select box for new objects.
$object_types = array(
'' => '- ' . t('select object') . ' -',
);
if ($handlers) {
foreach ($handlers as $key => $object_info) {
$class = $object_info['class'];
$max_object_count = call_user_func(array(
$class,
'getMaxOccurences',
));
$under_limit = !$max_object_count || !(isset($object_counts[$key]) && $object_counts[$key] >= $max_object_count);
if ($under_limit && empty($object_info['legacy'])) {
$object_types[$key] = $object_info['label'];
}
}
}
$form['more'] = array(
'#type' => 'markup',
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
);
$form['more']['add_another'] = array(
'#type' => 'button',
'#value' => t('Add object'),
'#ajax' => array(
'callback' => '::ajaxCallback',
'method' => 'replace',
'wrapper' => 'course-outline-wrapper',
),
'#weight' => 20,
);
// Sort course object types
asort($object_types);
$form['more']['object_type'] = array(
'#type' => 'select',
'#options' => $object_types,
'#weight' => 10,
);
$form['actions']['#type'] = 'actions';
// Submit and reset buttons.
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save outline'),
);
if (!empty($_SESSION['course'][$course
->id()]['editing'])) {
$form['actions']['reset'] = array(
'#type' => 'submit',
'#value' => t('Revert'),
'#submit' => [
'::resetForm',
],
);
}
$cform['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'course-object-weight',
];
return $form;
}