You are here

protected function GroupSelectorWidget::addGroupPicker in Opigno calendar event 8

Same name and namespace in other branches
  1. 3.x modules/opigno_calendar_event_group/src/GroupSelectorWidget.php \Drupal\opigno_calendar_event_group\GroupSelectorWidget::addGroupPicker()

Adds the groups selector.

Parameters

array $element: The form element corresponding to a single calendar event form.

\Drupal\group\Entity\GroupInterface[] $groups: An array of selectable groups.

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

1 call to GroupSelectorWidget::addGroupPicker()
GroupSelectorWidget::alterForm in modules/opigno_calendar_event_group/src/GroupSelectorWidget.php
Alters the parent form.

File

modules/opigno_calendar_event_group/src/GroupSelectorWidget.php, line 118

Class

GroupSelectorWidget
Provides a widget to select calendar event groups.

Namespace

Drupal\opigno_calendar_event_group

Code

protected function addGroupPicker(array &$element, array $groups, array $form, FormStateInterface $form_state) {
  if (!isset($element['displayed'])) {
    return;
  }
  $options = array_map(function (GroupInterface $group) {
    return $group
      ->label();
  }, $groups);
  $options['_displayed'] = $this
    ->t('Global calendar');
  $widget = $this->calendarEventManager
    ->getEmbeddedWidget();
  $state = $widget
    ->getItemState($form, $form_state, $element['#delta']);

  /** @var \Drupal\opigno_calendar_event\CalendarEventInterface $calendar_event */
  $calendar_event = $state['entity'];
  $default_values = array_map(function ($item) {
    return $item['target_id'];
  }, $calendar_event
    ->get('display_groups')
    ->getValue());
  $widget_element =& $element['displayed']['widget']['value'];
  if (!empty($widget_element['#default_value'])) {
    $default_values[] = '_displayed';
  }
  $widget_element = [
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $default_values,
  ] + $widget_element;
  $element['#entity_builders'][] = [
    $this,
    'buildEntity',
  ];
}