View source
<?php
namespace Drupal\opigno_learning_path\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Entity\Group;
use Drupal\user\Entity\User;
class LearningPathCreateClassForm extends FormBase {
public function getFormId() {
return 'learning_path_create_class_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#prefix'] = '<div id="learning_path_create_class_form">';
$form['#suffix'] = '</div>';
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Give a name to the class'),
'#placeholder' => $this
->t('Enter the class name'),
];
$form['search_label'] = [
'#type' => 'label',
'#title' => $this
->t('Add learners to the class'),
];
$group = $this
->getRequest()
->get('group');
$form['search'] = [
'#type' => 'textfield',
'#autocomplete_route_name' => 'opigno_learning_path.membership.add_user_to_class_autocomplete',
'#autocomplete_route_parameters' => [
'group' => $group !== NULL ? $group
->id() : 0,
],
'#placeholder' => $this
->t('Search name or email'),
'#attributes' => [
'id' => 'class_users_autocomplete',
],
];
$form['new_class_users'] = [
'#type' => 'multiselect',
'#attributes' => [
'id' => 'new_class_users',
'class' => [
'row',
],
],
'#options' => [],
'#validated' => TRUE,
'#process' => [
[
'Drupal\\multiselect\\Element\\MultiSelect',
'processSelect',
],
],
'#prefix' => '<div id="learning_path_create_class_form_messages" class="alert-danger"></div>',
];
$form['create'] = [
'#type' => 'submit',
'#value' => $this
->t('Create new class'),
'#attributes' => [
'class' => [
'use-ajax',
'btn_create',
],
],
'#ajax' => [
'callback' => [
$this,
'submitFormAjax',
],
'event' => 'click',
],
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#attached']['library'][] = 'opigno_learning_path/create_member';
return $form;
}
public function submitFormAjax(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$form = \Drupal::formBuilder()
->getForm('Drupal\\opigno_learning_path\\Form\\LearningPathCreateMemberForm');
if ($form_state->hasValidateError) {
$response
->addCommand(new HtmlCommand('#learning_path_create_class_form_messages', $this
->t('Please select at least one user')));
}
else {
$response
->addCommand(new ReplaceCommand('#learning_path_create_class_form', $form));
}
return $response;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (empty($form_state
->getValue('new_class_users'))) {
$form_state->hasValidateError = TRUE;
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$name = $form_state
->getValue('name');
$users = $form_state
->getValue('new_class_users');
$uids = array_map(function ($user) {
list($type, $id) = explode('_', $user);
return $id;
}, $users);
$users = User::loadMultiple($uids);
$class = Group::create([
'type' => 'opigno_class',
'label' => $name,
]);
$class
->save();
$group = $this
->getRequest()
->get('group');
$group
->addContent($class, 'subgroup:opigno_class');
foreach ($users as $user) {
if (!isset($user)) {
continue;
}
$class
->addMember($user);
}
foreach ($users as $user) {
if (!isset($user)) {
continue;
}
$group
->addMember($user);
}
$this
->messenger()
->addMessage(t('New class created'));
}
}