You are here

public function CalendarEventEmbeddedWidget::alterForm in Opigno calendar event 8

Same name in this branch
  1. 8 src/CalendarEventEmbeddedWidget.php \Drupal\opigno_calendar_event\CalendarEventEmbeddedWidget::alterForm()
  2. 8 src/Form/CalendarEventEmbeddedWidget.php \Drupal\opigno_calendar_event\Form\CalendarEventEmbeddedWidget::alterForm()
Same name and namespace in other branches
  1. 3.x src/CalendarEventEmbeddedWidget.php \Drupal\opigno_calendar_event\CalendarEventEmbeddedWidget::alterForm()

Alters the parent form.

Parameters

array $form: The form array.

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

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The definition of the entity reference field relating a calendar event with the entity being edited.

\Drupal\opigno_calendar_event\CalendarEventInterface[] $calendar_events: An array of calendar event entities referencing the entity being edited.

File

src/CalendarEventEmbeddedWidget.php, line 67

Class

CalendarEventEmbeddedWidget
Provides a widget to attach a calendar event to an entity.

Namespace

Drupal\opigno_calendar_event

Code

public function alterForm(array &$form, FormStateInterface $form_state, FieldDefinitionInterface $field_definition, array $calendar_events) {
  $widget_state =& $this
    ->getWidgetState($form, $form_state);
  $widget_state['field_definition'] = $field_definition;

  /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  $widget_state['original_label'] = $form_object
    ->getEntity()
    ->label();
  $elements =& $form[static::ELEMENT_NAME];
  $elements = [
    '#parents' => $this
      ->getElementParents($form),
    '#tree' => TRUE,
  ];
  if (count($calendar_events) > 1) {
    $elements += [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Calendar events'),
    ];
  }

  /** @var \Drupal\opigno_calendar_event\CalendarEventInterface $calendar_event */
  foreach (array_values($calendar_events) as $delta => $calendar_event) {
    $elements[$delta] = [
      '#parents' => $this
        ->getElementParents($form, $delta),
      '#tree' => TRUE,
      '#delta' => $delta,
      '#entity_builders' => [
        [
          $this,
          'buildEntity',
        ],
      ],
    ];
    $item_state =& $this
      ->getItemState($form, $form_state, $delta);
    $item_state['entity'] = $calendar_event;
    $form_display = $this
      ->getItemFormDisplay($form, $form_state, $delta);
    $form_display
      ->buildForm($calendar_event, $elements[$delta], $form_state);
    $this
      ->disableRequired($elements[$delta]);
    $this
      ->attachFieldGroups($elements[$delta], $calendar_event, $form_display);
  }
  $form['#validate'][] = [
    $this,
    'validateForm',
  ];
  $form['actions']['submit']['#submit'][] = [
    $this,
    'submitForm',
  ];
}