You are here

public function CalendarEventEmbeddedWidget::buildEntity in Opigno calendar event 8

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

Entity builder method.

Parameters

string $entity_type_id: The entity type ID.

\Drupal\calendar_event\CalendarEventInterface $calendar_event: The calendar event entity to be built.

array $element: The subform element.

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

File

src/Form/CalendarEventEmbeddedWidget.php, line 166

Class

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

Namespace

Drupal\opigno_calendar_event\Form

Code

public function buildEntity($entity_type_id, CalendarEventInterface $calendar_event, array &$element, FormStateInterface $form_state) {
  $widget_state = $this
    ->getWidgetState($form_state
    ->getCompleteForm(), $form_state);

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $widget_state['field_definition'];
  $ref_field_name = $field_definition
    ->getName();

  /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  $referenced_entity = $form_object
    ->getEntity();
  $calendar_event
    ->set($ref_field_name, $referenced_entity);

  // Make sure the calendar event has a valid title and keep the calendar
  // event label synchronized with the referenced entity, unless they are
  // explicitly diverging.
  $calendar_event_label = $calendar_event
    ->label();
  if (!$calendar_event_label || $widget_state['original_label'] === $calendar_event_label) {
    $label = $referenced_entity
      ->label();
  }
  else {
    $label = $calendar_event_label;
  }
  $calendar_event
    ->set('title', $label ?: $this
    ->t('Calendar event'));
}