CourseManageForm.php in Course 3.x
File
src/Form/CourseManageForm.php
View source
<?php
namespace Drupal\course\Form;
use Drupal\Core\Form\FormBase;
use Drupal\course\Entity\Course;
use Drupal;
use Drupal\Core\Form\FormStateInterface;
class CourseManageForm extends FormBase {
public function getFormId() : string {
return 'course_manage';
}
public function buildForm(array $form, FormStateInterface $form_state, Course $course = NULL) {
$collection = 'config.entity.key_store.course';
$name = 'course:' . $course
->id();
$kv = Drupal::keyValue($collection);
$course_settings = $kv
->get($name);
$form['status'] = [
'#title' => $this
->t('Enrollments are'),
'#type' => 'select',
'#options' => [
1 => t('Open'),
0 => t('Closed'),
],
'#default_value' => $course_settings['status'],
];
$form['limit'] = [
'#title' => t('Limit'),
'#type' => 'number',
'#description' => t('No enrollments will be allowed after this limit is reached.'),
'#default_value' => $course_settings['limit'],
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save configuration'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$course = $form_state
->getBuildInfo()['args'][0];
$collection = 'config.entity.key_store.course';
$name = 'course:' . $course
->id();
$kv = Drupal::keyValue($collection);
$course_settings = $kv
->get($name);
$form_state
->cleanValues();
foreach ($form_state
->getValues() as $key => $value) {
$course_settings[$key] = $value;
}
$kv
->set($name, $course_settings);
$this
->messenger()
->addStatus(t('The configuration options have been saved.'));
}
}