/** * Displays the aggregator administration page. * * @return array * A render array as expected by drupal_render(). */ public function adminOverview() { $entity_manager = $this->entityManager(); $feeds = $entity_manager->getStorage('aggregator_feed')->loadMultiple(); $header = array($this->t('Title'), $this->t('Items'), $this->t('Last update'), $this->t('Next update'), $this->t('Operations')); $rows = array(); /** @var \Drupal\aggregator\FeedInterface[] $feeds */ foreach ($feeds as $feed) { $row = array(); $row[] = $feed->link(); $row[] = $this->formatPlural($entity_manager->getStorage('aggregator_item')->getItemCount($feed), '1 item', '@count items'); $last_checked = $feed->getLastCheckedTime(); $refresh_rate = $feed->getRefreshRate(); $row[] = $last_checked ? $this->t('@time ago', array('@time' => $this->dateFormatter->formatTimeDiffSince($last_checked))) : $this->t('never'); $row[] = $last_checked && $refresh_rate ? $this->t('@time left', array('@time' => $this->dateFormatter->formatTimeDiffUntil($last_checked + $refresh_rate))) : $this->t('never'); $links['edit'] = ['title' => $this->t('Edit'), 'url' => Url::fromRoute('entity.aggregator_feed.edit_form', ['aggregator_feed' => $feed->id()])]; $links['delete'] = array('title' => $this->t('Delete'), 'url' => Url::fromRoute('entity.aggregator_feed.delete_form', ['aggregator_feed' => $feed->id()])); $links['delete_items'] = array('title' => $this->t('Delete items'), 'url' => Url::fromRoute('aggregator.feed_items_delete', ['aggregator_feed' => $feed->id()])); $links['update'] = array('title' => $this->t('Update items'), 'url' => Url::fromRoute('aggregator.feed_refresh', ['aggregator_feed' => $feed->id()])); $row[] = array('data' => array('#type' => 'operations', '#links' => $links)); $rows[] = $row; } $build['feeds'] = array('#prefix' => '<h3>' . $this->t('Feed overview') . '</h3>', '#type' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => $this->t('No feeds available. <a href="@link">Add feed</a>.', array('@link' => $this->url('aggregator.feed_add')))); return $build; }
/** * Formats a timestamp. * * @param int $timestamp * A UNIX timestamp to format. * * @return string * The formatted timestamp string using the past or future format setting. */ protected function formatTimestamp($timestamp) { $granularity = $this->getSetting('granularity'); $options = ['granularity' => $granularity]; if ($this->request->server->get('REQUEST_TIME') > $timestamp) { return SafeMarkup::format($this->getSetting('past_format'), ['@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, $options)]); } else { return SafeMarkup::format($this->getSetting('future_format'), ['@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, $options)]); } }
/** * Tests the formatTimeDiffSince method. * * @covers ::formatTimeDiffSince */ public function testFormatTimeDiffSince() { $expected = '1 second'; $timestamp = $this->createTimestamp('2013-12-11 10:09:07'); $request_time = $this->createTimestamp('2013-12-11 10:09:08'); $options = array(); // Mocks the formatDiff function of the dateformatter object. $this->dateFormatterStub->expects($this->any())->method('formatDiff')->with($request_time, $timestamp, $options)->will($this->returnValue($expected)); $request = Request::createFromGlobals(); $request->server->set('REQUEST_TIME', $request_time); // Mocks a the request stack getting the current request. $this->requestStack->expects($this->any())->method('getCurrentRequest')->willReturn($request); $this->assertEquals($expected, $this->dateFormatterStub->formatTimeDiffUntil($timestamp, $options)); }
/** * {@inheritdoc} */ public function render(ResultRow $values) { $value = $this->getValue($values); $format = $this->options['date_format']; if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) { $custom_format = $this->options['custom_date_format']; } if ($value) { $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL; $time_diff = REQUEST_TIME - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence) switch ($format) { case 'raw time ago': return $this->dateFormatter->formatTimeDiffSince($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'time ago': return $this->t('%time ago', array('%time' => $this->dateFormatter->formatTimeDiffSince($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)))); case 'raw time hence': return $this->dateFormatter->formatTimeDiffUntil($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'time hence': return $this->t('%time hence', array('%time' => $this->dateFormatter->formatTimeDiffUntil($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)))); case 'raw time span': return ($time_diff < 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'inverse time span': return ($time_diff > 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'time span': $time = $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2)); return $time_diff < 0 ? $this->t('%time hence', array('%time' => $time)) : $this->t('%time ago', array('%time' => $time)); case 'custom': if ($custom_format == 'r') { return format_date($value, $format, $custom_format, $timezone, 'en'); } return format_date($value, $format, $custom_format, $timezone); default: return format_date($value, $format, '', $timezone); } } }