"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.1.0-rc1/core/modules/views/src/Plugin/views/field/Date.php" (18 Nov 2020, 7591 Bytes) of package /linux/www/drupal-9.1.0-rc1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Date.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

    1 <?php
    2 
    3 namespace Drupal\views\Plugin\views\field;
    4 
    5 use Drupal\Core\Entity\EntityStorageInterface;
    6 use Drupal\Core\Form\FormStateInterface;
    7 use Drupal\views\ResultRow;
    8 use Symfony\Component\DependencyInjection\ContainerInterface;
    9 use Drupal\Core\Datetime\DateFormatterInterface;
   10 
   11 /**
   12  * A handler to provide proper displays for dates.
   13  *
   14  * @ingroup views_field_handlers
   15  *
   16  * @ViewsField("date")
   17  */
   18 class Date extends FieldPluginBase {
   19 
   20   /**
   21    * The date formatter service.
   22    *
   23    * @var \Drupal\Core\Datetime\DateFormatterInterface
   24    */
   25   protected $dateFormatter;
   26 
   27   /**
   28    * The date format storage.
   29    *
   30    * @var \Drupal\Core\Entity\EntityStorageInterface
   31    */
   32   protected $dateFormatStorage;
   33 
   34   /**
   35    * Constructs a new Date object.
   36    *
   37    * @param array $configuration
   38    *   A configuration array containing information about the plugin instance.
   39    * @param string $plugin_id
   40    *   The plugin ID for the plugin instance.
   41    * @param mixed $plugin_definition
   42    *   The plugin implementation definition.
   43    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   44    *   The date formatter service.
   45    * @param \Drupal\Core\Entity\EntityStorageInterface $date_format_storage
   46    *   The date format storage.
   47    */
   48   public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, EntityStorageInterface $date_format_storage) {
   49     parent::__construct($configuration, $plugin_id, $plugin_definition);
   50 
   51     $this->dateFormatter = $date_formatter;
   52     $this->dateFormatStorage = $date_format_storage;
   53   }
   54 
   55   /**
   56    * {@inheritdoc}
   57    */
   58   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
   59     return new static(
   60       $configuration,
   61       $plugin_id,
   62       $plugin_definition,
   63       $container->get('date.formatter'),
   64       $container->get('entity_type.manager')->getStorage('date_format')
   65     );
   66   }
   67 
   68   /**
   69    * {@inheritdoc}
   70    */
   71   protected function defineOptions() {
   72     $options = parent::defineOptions();
   73 
   74     $options['date_format'] = ['default' => 'small'];
   75     $options['custom_date_format'] = ['default' => ''];
   76     $options['timezone'] = ['default' => ''];
   77 
   78     return $options;
   79   }
   80 
   81   /**
   82    * {@inheritdoc}
   83    */
   84   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
   85 
   86     $date_formats = [];
   87     foreach ($this->dateFormatStorage->loadMultiple() as $machine_name => $value) {
   88       $date_formats[$machine_name] = $this->t('@name format: @date', ['@name' => $value->label(), '@date' => $this->dateFormatter->format(REQUEST_TIME, $machine_name)]);
   89     }
   90 
   91     $form['date_format'] = [
   92       '#type' => 'select',
   93       '#title' => $this->t('Date format'),
   94       '#options' => $date_formats + [
   95         'custom' => $this->t('Custom'),
   96         'raw time ago' => $this->t('Time ago'),
   97         'time ago' => $this->t('Time ago (with "ago" appended)'),
   98         'raw time hence' => $this->t('Time hence'),
   99         'time hence' => $this->t('Time hence (with "hence" appended)'),
  100         'raw time span' => $this->t('Time span (future dates have "-" prepended)'),
  101         'inverse time span' => $this->t('Time span (past dates have "-" prepended)'),
  102         'time span' => $this->t('Time span (with "ago/hence" appended)'),
  103       ],
  104       '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
  105     ];
  106     $form['custom_date_format'] = [
  107       '#type' => 'textfield',
  108       '#title' => $this->t('Custom date format'),
  109       '#description' => $this->t('If "Custom", see <a href="https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters" target="_blank">the PHP docs</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.'),
  110       '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
  111     ];
  112     // Setup #states for all possible date_formats on the custom_date_format form element.
  113     foreach (['custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'] as $custom_date_possible) {
  114       $form['custom_date_format']['#states']['visible'][] = [
  115         ':input[name="options[date_format]"]' => ['value' => $custom_date_possible],
  116       ];
  117     }
  118     $form['timezone'] = [
  119       '#type' => 'select',
  120       '#title' => $this->t('Timezone'),
  121       '#description' => $this->t('Timezone to be used for date output.'),
  122       '#options' => ['' => $this->t('- Default site/user timezone -')] + system_time_zones(FALSE, TRUE),
  123       '#default_value' => $this->options['timezone'],
  124     ];
  125     foreach (array_merge(['custom'], array_keys($date_formats)) as $timezone_date_formats) {
  126       $form['timezone']['#states']['visible'][] = [
  127         ':input[name="options[date_format]"]' => ['value' => $timezone_date_formats],
  128       ];
  129     }
  130 
  131     parent::buildOptionsForm($form, $form_state);
  132   }
  133 
  134   /**
  135    * {@inheritdoc}
  136    */
  137   public function render(ResultRow $values) {
  138     $value = $this->getValue($values);
  139     $format = $this->options['date_format'];
  140     if (in_array($format, ['custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'])) {
  141       $custom_format = $this->options['custom_date_format'];
  142     }
  143 
  144     if ($value) {
  145       $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
  146       // Will be positive for a datetime in the past (ago), and negative for a
  147       // datetime in the future (hence).
  148       $time_diff = REQUEST_TIME - $value;
  149       switch ($format) {
  150         case 'raw time ago':
  151           return $this->dateFormatter->formatTimeDiffSince($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2]);
  152 
  153         case 'time ago':
  154           return $this->t('%time ago', ['%time' => $this->dateFormatter->formatTimeDiffSince($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2])]);
  155 
  156         case 'raw time hence':
  157           return $this->dateFormatter->formatTimeDiffUntil($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2]);
  158 
  159         case 'time hence':
  160           return $this->t('%time hence', ['%time' => $this->dateFormatter->formatTimeDiffUntil($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2])]);
  161 
  162         case 'raw time span':
  163           return ($time_diff < 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, ['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);
  164 
  165         case 'inverse time span':
  166           return ($time_diff > 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, ['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);
  167 
  168         case 'time span':
  169           $time = $this->dateFormatter->formatTimeDiffSince($value, ['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);
  170           return ($time_diff < 0) ? $this->t('%time hence', ['%time' => $time]) : $this->t('%time ago', ['%time' => $time]);
  171 
  172         case 'custom':
  173           if ($custom_format == 'r') {
  174             return $this->dateFormatter->format($value, $format, $custom_format, $timezone, 'en');
  175           }
  176           return $this->dateFormatter->format($value, $format, $custom_format, $timezone);
  177 
  178         default:
  179           return $this->dateFormatter->format($value, $format, '', $timezone);
  180       }
  181     }
  182   }
  183 
  184 }