From a2b6a2d29e9a67dc26d93d33df55fb547fcde99f Mon Sep 17 00:00:00 2001
From: Anas Mawlawi <anas.mawlawi89@gmail.com>
Date: Tue, 12 Dec 2017 10:52:17 +0200
Subject: [PATCH] Add field formatter

---
 .../FieldFormatter/TaarikhHijriFieldFormatter.php  | 47 ++++++++++++++++++++++
 .../TaarikhAlgorithmPluginBase.php                 |  2 +-
 taarikh.module                                     |  8 +++-
 templates/taarikh.html.twig                        |  2 +-
 4 files changed, 55 insertions(+), 4 deletions(-)
 create mode 100644 src/Plugin/Field/FieldFormatter/TaarikhHijriFieldFormatter.php

diff --git a/src/Plugin/Field/FieldFormatter/TaarikhHijriFieldFormatter.php b/src/Plugin/Field/FieldFormatter/TaarikhHijriFieldFormatter.php
new file mode 100644
index 0000000..45f6e11
--- /dev/null
+++ b/src/Plugin/Field/FieldFormatter/TaarikhHijriFieldFormatter.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Drupal\taarikh\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Field\FormatterBase;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\taarikh\Plugin\TaarikhAlgorithm\FatimidAstronomical;
+
+/**
+ * Plugin implementation of the 'hijri_date' formatter.
+ *
+ * @FieldFormatter(
+ *   id = "taarikh_default",
+ *   label = @Translation("Hijri Date and Time"),
+ *   field_types = {
+ *     "datetime"
+ *   }
+ * )
+ */
+class TaarikhHijriFieldFormatter extends FormatterBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function viewElements(FieldItemListInterface $items, $langcode) {
+    $elements = [];
+
+    foreach ($items as $delta => $item) {
+      if ($item->date) {
+        $className = ['class' => "Hussainweb\DateConverter\Strategy\Algorithm\Hijri\HijriFatimidAstronomical"];
+        $FatimidAstronomicalDate = new FatimidAstronomical($className, 'HijriFatimidAstronomical', 'HijriFatimidAstronomical');
+        $date = $FatimidAstronomicalDate->fromDrupalDateTime($item->date);
+
+        $elements[$delta] = [
+          '#theme' => 'taarikh',
+          '#year' => $date->getYear(),
+          '#month' => $date->getMonth(),
+          '#day' => $date->getMonthDay(),
+        ];
+      }
+    }
+
+    return $elements;
+  }
+
+}
diff --git a/src/Plugin/TaarikhAlgorithm/TaarikhAlgorithmPluginBase.php b/src/Plugin/TaarikhAlgorithm/TaarikhAlgorithmPluginBase.php
index 7aad0e9..c823318 100644
--- a/src/Plugin/TaarikhAlgorithm/TaarikhAlgorithmPluginBase.php
+++ b/src/Plugin/TaarikhAlgorithm/TaarikhAlgorithmPluginBase.php
@@ -5,8 +5,8 @@ namespace Drupal\taarikh\Plugin\TaarikhAlgorithm;
 use Drupal\Core\Datetime\DrupalDateTime;
 use Drupal\Core\Plugin\PluginBase;
 use Drupal\taarikh\TaarikhAlgorithmPluginInterface;
-use Hussainweb\DateConverter\Strategy\The;
 use Hussainweb\DateConverter\Value\DateInterface;
+use Hussainweb\DateConverter\Strategy\Algorithm\Hijri\HijriFatimidAstronomical;

 /**
  * The base class for all taarikh algorithm plugins.
diff --git a/taarikh.module b/taarikh.module
index c8a12b6..47c8a38 100644
--- a/taarikh.module
+++ b/taarikh.module
@@ -25,7 +25,11 @@ function taarikh_theme() {
   return [
     'taarikh' => [
       'template' => 'taarikh',
-      'render element' => 'children',
-    ],
+      'variables' => [
+        'year' => NULL,
+        'month' => NULL,
+        'day' => NULL,
+      ],
+    ]
   ];
 }
diff --git a/templates/taarikh.html.twig b/templates/taarikh.html.twig
index 91e43c8..3ce199d 100644
--- a/templates/taarikh.html.twig
+++ b/templates/taarikh.html.twig
@@ -1 +1 @@
-<!-- Add you custom twig html here -->
\ No newline at end of file
+<p>{{ day }}/{{ month }}/{{ year }}</p>
--
2.15.0
