public function MembersPlugin::getMembersForm in Opigno Learning path 8
Same name and namespace in other branches
- 3.x src/Plugin/LearningPathMembers/MembersPlugin.php \Drupal\opigno_learning_path\Plugin\LearningPathMembers\MembersPlugin::getMembersForm()
Get members form.
Parameters
array $form: Form array.
mixed $form_state: Form state.
mixed $current_user: User object.
Return value
mixed From.
Overrides LearningPathMembersPluginBase::getMembersForm
File
- src/
Plugin/ LearningPathMembers/ MembersPlugin.php, line 22
Class
- MembersPlugin
- Class MembersPlugin.
Namespace
Drupal\opigno_learning_path\Plugin\LearningPathMembersCode
public function getMembersForm(array &$form, FormStateInterface $form_state, User $current_user) {
$storage = $form_state
->getStorage();
// If user can add any other users or only from his groups.
$show_all = $current_user
->hasPermission('add any members to calendar event') ? TRUE : FALSE;
$storage['show_all'] = $show_all;
// Add filters for the members field.
$form['members'] = [
'#type' => 'container',
'#weight' => 100,
];
$form['members']['title'] = [
'#type' => 'label',
'#title' => t('Members'),
];
$form['members']['filters'] = [
'#type' => 'container',
];
/** @var \Drupal\group\Entity\Group[] $classes */
$classes = \Drupal::entityTypeManager()
->getStorage('group')
->loadByProperties([
'type' => 'opigno_class',
]);
$options = [];
foreach ($classes as $class) {
if ($show_all || $class
->getMember($current_user) !== FALSE) {
$options[$class
->id()] = $class
->label();
}
}
uasort($options, 'strcasecmp');
$options = [
'All' => t('Filter by class'),
] + $options;
$form['members']['filters']['class'] = [
'#type' => 'select',
'#wrapper_attributes' => [
'class' => [
'',
],
],
'#options' => $options,
'#default_value' => t('All'),
'#ajax' => [
'callback' => [
$this,
'updateMembersAjax',
],
'event' => 'change',
'wrapper' => 'members',
'progress' => [
'type' => 'throbber',
'message' => t('Verifying entry...'),
],
],
];
/** @var \Drupal\group\Entity\Group[] $trainings */
$trainings = \Drupal::entityTypeManager()
->getStorage('group')
->loadByProperties([
'type' => 'learning_path',
]);
$options = [];
foreach ($trainings as $training) {
if ($show_all || $training
->getMember($current_user) !== FALSE) {
$options[$training
->id()] = $training
->label();
}
}
uasort($options, 'strcasecmp');
$options = [
'All' => t('Filter by training'),
] + $options;
$form['members']['filters']['training'] = [
'#type' => 'select',
'#wrapper_attributes' => [
'class' => [
'',
],
],
'#options' => $options,
'#default_value' => t('All'),
'#ajax' => [
'callback' => [
$this,
'updateMembersAjax',
],
'event' => 'change',
'wrapper' => 'members',
'progress' => [
'type' => 'throbber',
'message' => t('Verifying entry...'),
],
],
];
// Get the users for the specific group.
$users = opigno_messaging_get_all_recipients($show_all);
$allowed_uids = [];
foreach ($users as $user) {
$allowed_uids[] = $user
->id();
}
if ($allowed_uids) {
$allowed_uids = array_unique($allowed_uids);
// Save to form storage.
$storage['allowed_uids'] = $allowed_uids;
// Filter allowed users.
if ($options = $form["field_calendar_event_members"]["widget"]["#options"]) {
foreach ($options as $key => $option) {
if (!in_array($key, $allowed_uids)) {
unset($form["field_calendar_event_members"]["widget"]["#options"][$key]);
}
}
}
}
$form['members']['field_calendar_event_members'] = $form['field_calendar_event_members'];
unset($form['field_calendar_event_members']);
$members =& $form['members']['field_calendar_event_members'];
$members['#prefix'] = '<div id="members">';
$members['#suffix'] = '</div>';
unset($members['widget']['#title']);
$form_state
->setStorage($storage);
if (!$current_user
->hasPermission('add members to calendar event')) {
// Hide calendar events members field.
if (!empty($form["field_calendar_event_members"])) {
$form["field_calendar_event_members"]["#access"] = FALSE;
}
if (!empty($form['members'])) {
$form['members']['#access'] = FALSE;
}
}
}