summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrobin.ingelbrecht2017-06-11 17:30:28 +0200
committerrobin.ingelbrecht2017-06-11 17:30:28 +0200
commit87d31db275ebe8cf7e12be2d4ed3c56e5f45a75e (patch)
tree10f90bba3c060c0243f2cc31fe4e106870b073fc
parentb6a53835499388dea49b55bd4dc7c65e406192ef (diff)
Added formatter8.x-1.0
-rw-r--r--README.md7
-rw-r--r--src/Plugin/Field/FieldFormatter/DateTimeFlipClockFormatter.php152
2 files changed, 158 insertions, 1 deletions
diff --git a/README.md b/README.md
index 9b1022f..c216fd5 100644
--- a/README.md
+++ b/README.md
@@ -43,4 +43,9 @@ exposes a new formatter that can be uqsed on date fields
* Navigate to `admin/structure/block`
* Click `place block`
-* Add a new instance of the block "Clock" and configure it. \ No newline at end of file
+* Add a new instance of the block "Clock" and configure it.
+
+### Formatter
+
+* Navigate to the Manage display tab of your entity and change to formatter to "FlipClock"
+* Configure formatter and save \ No newline at end of file
diff --git a/src/Plugin/Field/FieldFormatter/DateTimeFlipClockFormatter.php b/src/Plugin/Field/FieldFormatter/DateTimeFlipClockFormatter.php
new file mode 100644
index 0000000..72ab0c3
--- /dev/null
+++ b/src/Plugin/Field/FieldFormatter/DateTimeFlipClockFormatter.php
@@ -0,0 +1,152 @@
+<?php
+
+namespace Drupal\flipclock\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Datetime\DrupalDateTime;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Field\FormatterBase;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Plugin implementation of the 'FlipClock' formatter for 'datetime' fields.
+ *
+ * @FieldFormatter(
+ * id = "flipclock_clock",
+ * label = @Translation("FlipClock"),
+ * field_types = {
+ * "datetime"
+ * }
+ *)
+ */
+class DateTimeFlipClockFormatter extends FormatterBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultSettings() {
+ return [
+ 'clock_face' => 'HourlyCounter',
+ 'auto_play' => TRUE,
+ 'auto_start' => TRUE,
+ 'countdown' => FALSE,
+ 'show_seconds' => TRUE,
+ ] + parent::defaultSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function viewElements(FieldItemListInterface $items, $langcode) {
+ $elements = [];
+
+ foreach ($items as $delta => $item) {
+ if ($item->date) {
+ /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
+ $date = $item->date;
+
+ $id = 'flipclock-' . uniqid();
+
+ $instance = [
+ 'timestamp' => (int) strtotime($date->format("Y-m-d\TH:i:s") . 'Z'),
+ 'options' => [
+ 'clockFace' => $this->getSetting('clock_face'),
+ 'autoPlay' => $this->getSetting('auto_play') ? TRUE : FALSE,
+ 'autoStart' => $this->getSetting('auto_start') ? TRUE : FALSE,
+ 'countdown' => $this->getSetting('countdown') ? TRUE : FALSE,
+ 'showSeconds' => $this->getSetting('show_Seconds') ? TRUE : FALSE,
+ ],
+ ];
+
+ $build = [
+ '#theme' => 'flipclock',
+ '#id' => $id,
+ '#attached' => [
+ 'library' => ['flipclock/flipclock.load'],
+ ],
+ ];
+
+ $build['#attached']['drupalSettings']['flipClock']['instances'][$id] = $instance;
+ $elements[$delta] = $build;
+ }
+
+ }
+
+ return $elements;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsForm(array $form, FormStateInterface $form_state) {
+ $form = parent::settingsForm($form, $form_state);
+
+ $form['clock_face'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Clock'),
+ '#options' => [
+ 'HourlyCounter' => $this->t('Hourly Counter'),
+ 'MinuteCounter' => $this->t('Minute Counter'),
+ 'DailyCounter' => $this->t('Daily Counter'),
+ 'TwelveHourClock' => $this->t('12hr Clock'),
+ 'TwentyFourHourClock' => $this->t('24hr Clock'),
+ ],
+ '#description' => $this->t('This is the name of the clock that is used to build the clock display.'),
+ '#required' => TRUE,
+ '#default_value' => $this->getSetting('clock_face'),
+ ];
+
+ $form['auto_play'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Auto play'),
+ '#description' => $this->t('Indicate if the clock should automatically add the play class to start the animation.'),
+ '#default_value' => $this->getSetting('auto_play'),
+ ];
+
+ $form['auto_start'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Auto start'),
+ '#description' => $this->t('Indicate if the clock should start automatically.'),
+ '#default_value' => $this->getSetting('auto_start'),
+ ];
+
+ $form['countdown'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Countdown'),
+ '#description' => $this->t('Indicate if the clock will count down instead of up.'),
+ '#default_value' => $this->getSetting('countdown'),
+ ];
+
+ $form['show_seconds'] = [
+ '#type' => 'checkbox',
+ '#title' => $this->t('Show seconds'),
+ '#description' => $this->t('Indicate if the clock should include a seconds display.'),
+ '#default_value' => $this->getSetting('show_seconds'),
+ ];
+
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsSummary() {
+ $summary = parent::settingsSummary();
+
+ $clocks = [
+ 'HourlyCounter' => $this->t('Hourly Counter'),
+ 'MinuteCounter' => $this->t('Minute Counter'),
+ 'DailyCounter' => $this->t('Daily Counter'),
+ 'TwelveHourClock' => $this->t('12hr Clock'),
+ 'TwentyFourHourClock' => $this->t('24hr Clock'),
+ ];
+
+ $summary[] = $this->t('Clock: @clock', ['@clock' => $clocks[$this->getSetting('clock_face')]]);
+ $summary[] = $this->t('Auto play: @auto_play', ['@auto_play' => $this->getSetting('auto_play') ? $this->t('Yes') : $this->t('No')]);
+ $summary[] = $this->t('Auto start: @auto_start', ['@auto_start' => $this->getSetting('auto_start') ? $this->t('Yes') : $this->t('No')]);
+ $summary[] = $this->t('Countdown: @countdown', ['@countdown' => $this->getSetting('countdown') ? $this->t('Yes') : $this->t('No')]);
+ $summary[] = $this->t('Show seconds: @show_seconds', ['@show_seconds' => $this->getSetting('show_seconds') ? $this->t('Yes') : $this->t('No')]);
+
+ return $summary;
+ }
+
+}