Commit c6cd2132 authored by git's avatar git Committed by Yahya Al Hamad
Browse files

Issue #3258061 by Abdalhamid: Hijri Formatter throws an error when the field date is date only

parent df64e476
Loading
Loading
Loading
Loading
−7
Changes for src/Form/HijriFormatSettings.php: 0 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -49,13 +49,6 @@ class HijriFormatSettings extends ConfigFormBase {
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
+10 −5
Changes for src/HijriFormatManager.php: 10 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ class HijriFormatManager {
  /**
   * Date Formatter.
   *
   * @var DateFormatter $dateFormatter
   * @var \Drupal\Core\Datetime\DateFormatter
   */

  private $dateFormatter;
@@ -22,7 +22,7 @@ class HijriFormatManager {
  /**
   * Config Factory.
   *
   * @var ConfigFactoryInterface $configFactory
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

@@ -43,8 +43,10 @@ class HijriFormatManager {
  /**
   * Constructs HijriFormatManager.
   *
   * @param DateFormatter $dateFormatter
   * @param ConfigFactoryInterface $configFactory
   * @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
   *   The date formatter.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   */
  public function __construct(DateFormatter $dateFormatter, ConfigFactoryInterface $configFactory) {
    $this->dateFormatter = $dateFormatter;
@@ -122,6 +124,8 @@ class HijriFormatManager {
   *   Date in timestamp.
   * @param string $format
   *   Date format.
   * @param int $is_indian
   *   If 1, return the converted indian date.
   */
  public function convertToHijri($timestamp, $format, $is_indian = 0) {

@@ -131,7 +135,8 @@ class HijriFormatManager {
    // Get Hijri date.
    $hijri = Hijri::convertToHijri(date('d-m-Y', $timestamp), $adjustment);

    // Replace supported formats with Hijri dates, this idea come to life to render dates through Drupal
    // Replace supported formats with Hijri dates,
    // this idea come to life to render dates through Drupal
    // So, the translation will be a matter of interface translation.
    $patterns = [
      0 => '/(?<!\\\\)d/',
+1 −2
Changes for src/Plugin/Block/HijriBlock.php: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\hijri_format\Plugin\Block;

use Drupal\Core\Block\Annotation\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@@ -22,7 +21,7 @@ class HijriBlock extends BlockBase implements ContainerFactoryPluginInterface {
  /**
   * Hijri Manager.
   *
   * @var hijriFormatManager $hijriFormatManager
   * @var hijriFormatManager
   */
  protected $hijriFormatManager;

+12 −10
Changes for src/Plugin/Field/FieldFormatter/HijriFieldFormatter.php: 12 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -2,15 +2,14 @@

namespace Drupal\hijri_format\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Annotation\FieldFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\hijri_format\HijriFormatManager;
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
use Drupal\hijri_format\HijriFormatManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'hijri_field_formatter' formatter.
@@ -31,7 +30,7 @@ class HijriFieldFormatter extends FormatterBase {
  /**
   * Hijri manager.
   *
   * @var HijriFormatManager $hijriFormatManager
   * @var \Drupal\hijri_format\HijriFormatManager
   */
  protected $hijriFormatManager;

@@ -122,12 +121,12 @@ class HijriFieldFormatter extends FormatterBase {
      $class = get_class($item);
      switch ($class) {
        case DateRangeItem::class:
          $date[] = $this->hijriFormatManager->convertToHijri($this->getUTCTimestamp($item->getValue()['value']), $format, $is_indian);
          $date[] = $this->hijriFormatManager->convertToHijri($this->getUTCTimestamp($item->getValue()['end_value']), $format, $is_indian);
          $date[] = $this->hijriFormatManager->convertToHijri($this->getUtcTimestamp($item->getValue()['value']), $format, $is_indian);
          $date[] = $this->hijriFormatManager->convertToHijri($this->getUtcTimestamp($item->getValue()['end_value']), $format, $is_indian);
          break;

        case DateTimeItem::class:
          $value = $this->getUTCTimestamp($item->getValue()['value']);
          $value = $this->getUtcTimestamp($item->getValue()['value']);
        default:
          $value = $value ?? $item->getValue()['value'];
          $date[] = $this->hijriFormatManager->convertToHijri($value, $format, $is_indian);
@@ -146,13 +145,16 @@ class HijriFieldFormatter extends FormatterBase {
   *
   * @param string $date_time_string
   *   DateTime string (e.g. 1996-04-24T12:00:00).
   *
   * @return int
   *   Timestamp using UTC timezone.
   */
  private function getUTCTimestamp(string $date_time_string) {
  private function getUtcTimestamp(string $date_time_string) {
    $timezone = new \DateTimeZone('UTC');
    $datetime_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('datetime_type');
    $date_time_string = str_replace("T", " ", $date_time_string);
    return \DateTime::createFromFormat('Y-m-d H:i:s', $date_time_string, $timezone)->getTimestamp();
    $format = $datetime_type === 'date' ? 'Y-m-d' : 'Y-m-d H:i:s';
    return \DateTime::createFromFormat($format, $date_time_string, $timezone)->getTimestamp();
  }

}