class DateTimeDatelistWidget

Plugin implementation of the 'datetime_datelist' widget.

Plugin annotation


@Plugin(
  id = "datetime_datelist",
  module = "datetime",
  label = @Translation("Select list"),
  field_types = {
    "datetime"
  },
  settings = {
    "increment" = 15,
    "date_order" = "YMD",
    "time_type" = "24",
  }
)

Hierarchy

Expanded class hierarchy of DateTimeDatelistWidget

File

drupal/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php, line 36
Contains \Drupal\datetime\Plugin\field\widget\DateTimeDatelistWidget.

Namespace

Drupal\datetime\Plugin\field\widget
View source
class DateTimeDatelistWidget extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public function __construct($plugin_id, array $plugin_definition, FieldInstance $instance, array $settings) {

    // Identify the function used to set the default value.
    $instance['default_value_function'] = $this
      ->defaultValueFunction();
    parent::__construct($plugin_id, $plugin_definition, $instance, $settings);
  }

  /**
   * Returns the callback used to set a date default value.
   *
   * @return string
   *   The name of the callback to use when setting a default date value.
   */
  public function defaultValueFunction() {
    return 'datetime_default_value';
  }

  /**
   * Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $field = $this->field;
    $instance = $this->instance;
    $date_order = $this
      ->getSetting('date_order');
    $time_type = $this
      ->getSetting('time_type');
    $increment = $this
      ->getSetting('increment');

    // We're nesting some sub-elements inside the parent, so we
    // need a wrapper. We also need to add another #title attribute
    // at the top level for ease in identifying this item in error
    // messages. We don't want to display this title because the
    // actual title display is handled at a higher level by the Field
    // module.
    $element['#theme_wrappers'][] = 'datetime_wrapper';
    $element['#attributes']['class'][] = 'container-inline';
    $element['#element_validate'][] = 'datetime_datelist_widget_validate';

    // Identify the type of date and time elements to use.
    switch ($field['settings']['datetime_type']) {
      case 'date':
        $storage_format = DATETIME_DATE_STORAGE_FORMAT;
        $type_type = 'none';
        break;
      default:
        $storage_format = DATETIME_DATETIME_STORAGE_FORMAT;
        break;
    }

    // Set up the date part order array.
    switch ($date_order) {
      case 'YMD':
        $date_part_order = array(
          'year',
          'month',
          'day',
        );
        break;
      case 'MDY':
        $date_part_order = array(
          'month',
          'day',
          'year',
        );
        break;
      case 'DMY':
        $date_part_order = array(
          'day',
          'month',
          'year',
        );
        break;
    }
    switch ($time_type) {
      case '24':
        $date_part_order = array_merge($date_part_order, array(
          'hour',
          'minute',
        ));
        break;
      case '12':
        $date_part_order = array_merge($date_part_order, array(
          'hour',
          'minute',
          'ampm',
        ));
        break;
      case 'none':
        break;
    }
    $element['value'] = array(
      '#type' => 'datelist',
      '#default_value' => NULL,
      '#date_increment' => $increment,
      '#date_part_order' => $date_part_order,
      '#date_timezone' => drupal_get_user_timezone(),
      '#required' => $element['#required'],
    );

    // Set the storage and widget options so the validation can use them. The
    // validator will not have access to field or instance settings.
    $element['value']['#date_storage_format'] = $storage_format;
    if (!empty($items[$delta]['date'])) {
      $date = $items[$delta]['date'];

      // The date was created and verified during field_load(), so it is safe to
      // use without further inspection.
      $date
        ->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
      if ($field['settings']['datetime_type'] == 'date') {

        // A date without time will pick up the current time, use the default
        // time.
        datetime_date_default_time($date);
      }
      $element['value']['#default_value'] = $date;
    }
    return $element;
  }

  /**
   *
   *
   * @param array $form
   *   The form definition as an array.
   * @param array $form_state
   *   The current state of the form as an array.
   *
   * @return array
   *
   */
  function settingsForm(array $form, array &$form_state) {
    $element = parent::settingsForm($form, $form_state);
    $field = $this->field;
    $instance = $this->instance;
    $element['date_order'] = array(
      '#type' => 'select',
      '#title' => t('Date part order'),
      '#default_value' => $this
        ->getSetting('date_order'),
      '#options' => array(
        'MDY' => t('Month/Day/Year'),
        'DMY' => t('Day/Month/Year'),
        'YMD' => t('Year/Month/Day'),
      ),
    );
    if ($field['settings']['datetime_type'] == 'datetime') {
      $element['time_type'] = array(
        '#type' => 'select',
        '#title' => t('Time type'),
        '#default_value' => $this
          ->getSetting('time_type'),
        '#options' => array(
          '24' => t('24 hour time'),
          '12' => t('12 hour time'),
        ),
      );
    }
    else {
      $element['time_type'] = array(
        '#type' => 'hidden',
        '#value' => 'none',
      );
    }
    $element['increment'] = array(
      '#type' => 'select',
      '#title' => t('Time increments'),
      '#default_value' => $this
        ->getSetting('increment'),
      '#options' => array(
        1 => t('1 minute'),
        5 => t('5 minute'),
        10 => t('10 minute'),
        15 => t('15 minute'),
        30 => t('30 minute'),
      ),
    );
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateTimeDatelistWidget::defaultValueFunction public function Returns the callback used to set a date default value.
DateTimeDatelistWidget::formElement public function Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). Overrides WidgetInterface::formElement
DateTimeDatelistWidget::settingsForm function Overrides WidgetBase::settingsForm
DateTimeDatelistWidget::__construct public function Constructs a WidgetBase object. Overrides WidgetBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). Overrides WidgetInterface::errorElement 5
WidgetBase::extractFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). Overrides WidgetBaseInterface::extractFormValues
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 2
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.