You are here

public function GroupSelectorWidget::alterForm in Opigno calendar event 3.x

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

Alters the parent form.

Parameters

array $form: The form array.

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

File

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

Class

GroupSelectorWidget
Provides a widget to select calendar event groups.

Namespace

Drupal\opigno_calendar_event_group

Code

public function alterForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $form_object
    ->getEntity();

  // If the event is new, it could be shown in the calendar of the group to
  // which it will be associated, otherwise we need to get all the selectable
  // groups associated to the event.
  $groups = [];
  if ($entity
    ->isNew()) {
    $group = $form_state
      ->get('group');
    if ($group instanceof GroupInterface) {
      $groups[$group
        ->id()] = $group;
    }
  }
  else {
    $groups = $this
      ->getSelectableGroups($entity);
  }
  if (!$groups) {
    return;
  }
  $elements =& $form[CalendarEventEmbeddedWidget::ELEMENT_NAME];
  foreach (Element::children($elements) as $delta) {
    if (is_int($delta)) {
      $this
        ->addGroupPicker($elements[$delta], $groups, $form, $form_state);
    }
  }
}