CronExpireDate.php in http:BL 8
File
src/Plugin/views/field/CronExpireDate.php
View source
<?php
namespace Drupal\httpbl\Plugin\views\field;
use Drupal\views\Plugin\views\field\Date;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
class CronExpireDate extends Date {
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$date_formats = array();
foreach ($this->dateFormatStorage
->loadMultiple() as $machine_name => $value) {
$date_formats[$machine_name] = $this
->t('@name format: @date', array(
'@name' => $value
->label(),
'@date' => $this->dateFormatter
->format(\Drupal::time()
->getRequestTime(), $machine_name),
));
}
$form['date_format'] = array(
'#type' => 'select',
'#title' => $this
->t('Date format'),
'#options' => $date_formats + array(
'cron time expire' => $this
->t('Cron expire date countdown (0 = "(next cron)")'),
),
'#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
);
foreach (array(
'cron time expire',
) as $custom_date_possible) {
$form['custom_date_format']['#states']['visible'][] = array(
':input[name="options[date_format]"]' => array(
'value' => $custom_date_possible,
),
);
}
foreach (array_merge(array(
'custom',
), array_keys($date_formats)) as $timezone_date_formats) {
$form['timezone']['#states']['visible'][] = array(
':input[name="options[date_format]"]' => array(
'value' => $timezone_date_formats,
),
);
}
}
public function render(ResultRow $values) {
$value = $this
->getValue($values);
$format = $this->options['date_format'];
if (in_array($format, array(
'cron time expire',
))) {
$custom_format = $this->options['custom_date_format'];
}
if ($value) {
$timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
$time_diff = \Drupal::time()
->getRequestTime() - $value;
switch ($format) {
case 'cron time expire':
if ($time_diff >= 0) {
return $this
->t('(next cron)');
}
else {
return $this
->t('in ' . $this->dateFormatter
->formatTimeDiffUntil($value, array(
'granularity' => is_numeric($custom_format) ? $custom_format : 2,
)));
}
default:
return format_date($value, $format, '', $timezone);
}
}
parent::render($values);
}
}
Classes
Name |
Description |
CronExpireDate |
Field handler to display the newer of last comment / node updated. |