TimeFormatter.php 2.7 KB
Newer Older
lostkoder's avatar
lostkoder committed
1
2
3
4
5
6
7
<?php

namespace Drupal\time_field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
8
use Drupal\Core\Form\FormStateInterface;
lostkoder's avatar
lostkoder committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use Drupal\time_field\Time;

/**
 * Plugin implementation of the 'time_formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "time_formatter",
 *   label = @Translation("Time formatter"),
 *   field_types = {
 *     "time"
 *   }
 * )
 */
class TimeFormatter extends FormatterBase {

  /**
   * Generate the output appropriate for one field item.
   *
   * @param \Drupal\Core\Field\FieldItemInterface $item
   *   One field item.
   *
   * @return string
   *   The textual output generated.
   */
  protected function viewValue(FieldItemInterface $item) {
    // The text value has no text format assigned to it, so the user input
    // should equal the output, including newlines.
    $time = Time::createFromTimestamp($item->value);
37
    return $time->format($this->getSetting('time_format'));
lostkoder's avatar
lostkoder committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];

    foreach ($items as $delta => $item) {
      $elements[$delta] = ['#markup' => $this->viewValue($item)];
    }

    return $elements;
  }
52

lostkoder's avatar
lostkoder committed
53
54
55
  /**
   * {@inheritdoc}
   */
56
57
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);
lostkoder's avatar
lostkoder committed
58
59
60
61
    $elements['time_format'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Time Format'),
      '#default_value' => (string) $this->getSetting('time_format'),
lostkoder's avatar
lostkoder committed
62
      '#description' => $this->getTimeDescription(),
63
64
65
66
67
68
69
70
71
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
lostkoder's avatar
lostkoder committed
72
73
      'time_format' => 'h:i a',
    ] + parent::defaultSettings();
74
  }
lostkoder's avatar
lostkoder committed
75

lostkoder's avatar
lostkoder committed
76
77
78
79
80
81
82
83
  /**
   * Description of the time field.
   *
   * @return string
   *   Description of the time field.
   */
  private function getTimeDescription() {
    $output = '';
84
85
86
87
88
89
90
91
92
    $output .= $this->t('a - Lowercase am or pm') . '<br/>';
    $output .= $this->t('A - Uppercase AM or PM') . '<br/>';
    $output .= $this->t('B - Swatch Internet time (000 to 999)') . '<br/>';
    $output .= $this->t('g - 12-hour format of an hour (1 to 12)') . '<br/>';
    $output .= $this->t('G - 24-hour format of an hour (0 to 23)') . '<br/>';
    $output .= $this->t('h - 12-hour format of an hour (01 to 12)') . '<br/>';
    $output .= $this->t('H - 24-hour format of an hour (00 to 23)') . '<br/>';
    $output .= $this->t('i - Minutes with leading zeros (00 to 59)') . '<br/>';
    $output .= $this->t('s - Seconds, with leading zeros (00 to 59)') . '<br/>';
lostkoder's avatar
lostkoder committed
93
94
95
    return $output;
  }

lostkoder's avatar
lostkoder committed
96
}