public function ILTForm::buildForm in Opigno Instructor-led Trainings 3.x
Same name and namespace in other branches
- 8 src/Form/ILTForm.php \Drupal\opigno_ilt\Form\ILTForm::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 EntityForm::buildForm
File
- src/
Form/ ILTForm.php, line 73
Class
- ILTForm
- Provides a form for creating/editing a opigno_ilt entity.
Namespace
Drupal\opigno_ilt\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
/** @var \Drupal\opigno_ilt\ILTInterface $entity */
$entity = $this->entity;
if ($entity
->getTraining() === NULL) {
$group = $this
->getRequest()
->get('group');
if ($group !== NULL) {
$group_type = $group
->getGroupType()
->id();
if ($group_type === 'learning_path') {
// If creating entity on a group page, set that group as a related.
$entity
->setTraining($group);
}
}
}
$form['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#default_value' => $entity
->label(),
'#required' => TRUE,
];
$date_field_def = $entity
->getFieldDefinition('date');
$date_field_item_list = $entity
->get('date');
$date_range_plugin_id = 'daterange_default';
$date_range = new OpignoDateRangeWidget($date_range_plugin_id, $this->pluginManager
->getDefinition($date_range_plugin_id), $date_field_def, array_merge(OpignoDateRangeWidget::defaultSettings(), [
'value_format' => 'Y-m-d H:i:s',
'value_timezone' => date_default_timezone_get(),
]), [], $this->entityTypeManager
->getStorage('date_format'));
$form['date'] = $date_range
->form($date_field_item_list, $form, $form_state);
$form['place'] = [
'#type' => 'textfield',
'#title' => $this
->t('Place'),
'#default_value' => $entity
->getPlace(),
'#placeholder' => $this
->t('Enter here the address where the instructor-led training will take place'),
'#required' => TRUE,
];
$training = $entity
->getTraining();
if ($training !== NULL) {
$trainer_id = $entity
->getTrainerId();
$trainer_name = '';
if ($trainer_id) {
$trainer = \Drupal::entityTypeManager()
->getStorage('user')
->load($trainer_id);
if ($trainer) {
$trainer_name = $trainer
->getAccountName();
}
}
$form['trainer'] = [
'#type' => 'textfield',
'#title' => $this
->t('Trainer'),
'#default_value' => $trainer_name,
'#autocomplete_route_name' => 'opigno_ilt.opigno_ilt_trainer_autocomplete',
'#autocomplete_route_parameters' => [
'group' => $training
->id(),
],
'#placeholder' => $this
->t('Enter a user’s name or email'),
];
$options = [];
$members = $entity
->getMembers();
foreach ($members as $member) {
$options['user_' . $member
->id()] = $this
->t("@name (User #@id)", [
'@name' => $member
->getDisplayName(),
'@id' => $member
->id(),
]);
}
$form['members'] = [
'#title' => $this
->t('Members restriction'),
'#type' => 'entity_selector',
'#attributes' => [
'id' => 'members',
'class' => [
'row',
],
],
'#default_value' => array_keys($options),
'#entity_selector_option' => '\\Drupal\\opigno_ilt\\Controller\\ILTController::membersAutocompleteSelect',
'#entity_selector_parameters' => [
'group' => $training,
],
'#multiple' => TRUE,
'#data_type' => 'key',
'#options' => [],
'#show_exists' => TRUE,
'#validated' => TRUE,
];
}
else {
$form['members'] = [
'#markup' => $this
->t('Instructor-Led Training should have a related training to add a members restriction.'),
];
}
$form['status_messages'] = [
'#type' => 'status_messages',
];
// $form['#attached']['library'][] = 'datepicker';
$form['#attached']['library'][] = 'opigno_ilt/form';
return $form;
}