You are here

DateTimeWidgetBase.php in Persian Date for Drupal 8 8.4

Same filename and directory in other branches
  1. 8 src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php

File

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

namespace Drupal\persian_date\Plugin\Field\FieldWidget;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\persian_date\Plugin\Datetime\PersianDrupalDateTime;

/**
 * Base class for the 'datetime_*' widgets.
 */
class DateTimeWidgetBase extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = [
      '#type' => 'datetime',
      '#default_value' => NULL,
      '#date_increment' => 1,
      '#date_timezone' => drupal_get_user_timezone(),
      '#required' => $element['#required'],
    ];
    if ($this
      ->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {

      // A date-only field should have no timezone conversion performed, so
      // use the same timezone as for storage.
      $element['value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE;
    }
    if ($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.
      if ($this
        ->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {

        // A date without time will pick up the current time, use the default
        // time.
        datetime_date_default_time($date);
      }
      $date
        ->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
      $date = PersianDrupalDateTime::createFromDrupalDateTime($date);
      $element['value']['#default_value'] = $date;
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

    // The widget form element type has transformed the value to a
    // DrupalDateTime object at this point. We need to convert it back to the
    // storage timezone and format.
    foreach ($values as &$item) {
      if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {
        $date = $item['value'];
        switch ($this
          ->getFieldSetting('datetime_type')) {
          case DateTimeItem::DATETIME_TYPE_DATE:

            // If this is a date-only field, set it to the default time so the
            // timezone conversion can be reversed.
            datetime_date_default_time($date);
            $format = DATETIME_DATE_STORAGE_FORMAT;
            break;
          default:
            $format = DATETIME_DATETIME_STORAGE_FORMAT;
            break;
        }

        // Adjust the date for storage.
        $date
          ->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
        $item['value'] = $date
          ->format($format);
      }
    }
    return $values;
  }

}

Classes

Namesort descending Description
DateTimeWidgetBase Base class for the 'datetime_*' widgets.