You are here

class PersianDateTime in Persian Date for Drupal 8 8.4

Same name and namespace in other branches
  1. 8 src/Element/PersianDateTime.php \Drupal\persian_date\Element\PersianDateTime

Class PersianDateTime @package Drupal\persian_date\Element

Plugin annotation

@FormElement("persian_datetime");

Hierarchy

Expanded class hierarchy of PersianDateTime

2 files declare their use of PersianDateTime
PersianTimestampDateTimeDefaultWidget.php in src/Plugin/Field/FieldWidget/PersianTimestampDateTimeDefaultWidget.php
TimestampDatetimeNoDefaultWidget.php in src/Plugin/Field/FieldWidget/TimestampDatetimeNoDefaultWidget.php

File

src/Element/PersianDateTime.php, line 23

Namespace

Drupal\persian_date\Element
View source
class PersianDateTime extends Datetime {
  public function getInfo() {
    $info = parent::getInfo();
    $info['#date_date_element'] = 'text';
    return $info;
  }
  public static function formatExample($format) {
    if (!static::$dateExample) {
      static::$dateExample = new PersianDrupalDateTime();
    }
    return static::$dateExample
      ->format($format);
  }
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input !== FALSE) {
      $date_input = $element['#date_date_element'] != 'none' && !empty($input['date']) ? $input['date'] : '';
      $time_input = $element['#date_time_element'] != 'none' && !empty($input['time']) ? $input['time'] : '';
      $date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
      $time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';
      $timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;

      // Seconds will be omitted in a post in case there's no entry.
      if (!empty($time_input) && strlen($time_input) == 5) {
        $time_input .= ':00';
      }
      try {
        $date_time_format = trim($date_format . ' ' . $time_format);
        $date_time_input = trim($date_input . ' ' . $time_input);
        $date = PersianDrupalDateTime::createFromFormat($date_time_format, $date_time_input, $timezone);
        $date = DrupalDateTime::createFromDateTime($date
          ->getDateTime());
      } catch (\Exception $e) {
        $date = NULL;
      }
      $input = [
        'date' => $date_input,
        'time' => $time_input,
        'object' => $date,
      ];
    }
    else {
      $date = $element['#default_value'];
      if ($date instanceof DrupalDateTime && !$date
        ->hasErrors()) {
        $input = [
          'date' => $date
            ->format($element['#date_date_format']),
          'time' => $date
            ->format($element['#date_time_format']),
          'object' => $date,
        ];
      }
      else {
        $input = [
          'date' => '',
          'time' => '',
          'object' => NULL,
        ];
      }
    }
    return $input;
  }

  /**
   * @inheritdoc
   */
  public static function processDatetime(&$element, FormStateInterface $form_state, &$complete_form) {

    // The value callback has populated the #value array.
    $date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
    if (is_object($date) && get_class($date) === DrupalDateTime::class) {
      $element['#value']['object'] = PersianDrupalDateTime::createFromDrupalDateTime($date);
    }
    return parent::processDatetime($element, $form_state, $complete_form);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateElementBase::datetimeRangeYears protected static function Specifies the start and end year to use as a date range.
DateElementBase::getElementTitle protected static function Returns the most relevant title of a datetime element.
Datetime::$dateExample protected static property
Datetime::getHtml5DateFormat protected static function Retrieves the right format for a HTML5 date element.
Datetime::getHtml5TimeFormat protected static function Retrieves the right format for a HTML5 time element.
Datetime::processAjaxForm public static function Form element processing handler for the #ajax form property. Overrides RenderElement::processAjaxForm
Datetime::validateDatetime public static function Validation callback for a datetime element.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PersianDateTime::formatExample public static function Creates an example for a date format. Overrides Datetime::formatExample
PersianDateTime::getInfo public function Returns the element properties for this element. Overrides Datetime::getInfo
PersianDateTime::processDatetime public static function @inheritdoc Overrides Datetime::processDatetime
PersianDateTime::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides Datetime::valueCallback
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.