You are here

OptionalEndDateDateRangeDatelistWidget.php in Optional End Date 8

File

src/Plugin/Field/FieldWidget/OptionalEndDateDateRangeDatelistWidget.php
View source
<?php

namespace Drupal\optional_end_date\Plugin\Field\FieldWidget;

use Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeDatelistWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Override the datelist widget.
 */
class OptionalEndDateDateRangeDatelistWidget extends DateRangeDatelistWidget {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    $optional_end_date = $this
      ->getFieldSetting('optional_end_date');
    $element['end_value']['#title'] = $optional_end_date ? $this
      ->t('End date (optional)') : $this
      ->t('End date');
    if ($element['#required'] && $optional_end_date) {
      $element['end_value']['#required'] = FALSE;
    }
    return $element;
  }

}

Classes

Namesort descending Description
OptionalEndDateDateRangeDatelistWidget Override the datelist widget.