0

In Drupal 8 I am using

$date_service = \Drupal::service('date');
$options = drupal_map_assoc(array(900, 1800, 3600, 7200, 10800, 21600, 32400, 43200,
  64800, 86400, 172800, 259200, 604800, 1209600, 2419200), array($date_service, 'formatInterval'));

But this gives me error

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "date". Did you mean one of these: "theme.negotiator.system.db_update", "entity.definition_update_manager", "date.formatter", "update.post_update_registry", "update.post_update_registry_factory", "access_check.db_update", "access_check.update.manager_access", "update.manager", "update.processor", "update.fetcher", "update.root"? in Drupal\Component\DependencyInjection\Container->get() (line 161 of /Applications/MAMP/htdocs/drupal-8.0.x-dev/core/lib/Drupal/Component/DependencyInjection/Container.php).

What wrong am i doing? Am i missing use class something like this use Drupal.... or something wrong in line $date_service = \Drupal::service('date');

5
  • 1
    There is no default service for date in drupal 8
    – Leopathu
    Commented Jul 19, 2016 at 10:30
  • In that case how do i use : $options = drupal_map_assoc(array(900 , .... .... , 2419200), array($date_service, 'formatInterval')); ?
    – neha
    Commented Jul 19, 2016 at 10:31
  • 1
    drupal.org/node/2207453 drupal_map_assoc has been removed from drupal 8
    – Leopathu
    Commented Jul 19, 2016 at 10:34
  • $options = array_map($callback, array_combine($my_array, $my_array)); you can use it :)
    – Leopathu
    Commented Jul 19, 2016 at 10:36
  • I was using $options = array_map($callback, array_combine($my_array, $my_array)); but it was not working, just now figured out i was not using proper $callback. Anyways thanks @Leopathu
    – neha
    Commented Jul 19, 2016 at 10:47

4 Answers 4

2

My piece of working code:

$time_intervals = array(30, 3600, 10800, 21600, 43200, 86400, 172800, 259200, 604800, 1209600, 2592000, 5184000, 7776000);

$options = $this->build_options($time_intervals);

Also i have defined following function in the same class:

function build_options(array $time_intervals, $granularity = 2, $langcode = NULL) {
  $callback = function ($value) use ($granularity, $langcode) {
    return \Drupal::service('date.formatter')->formatInterval($value, $granularity, $langcode);
  };

  return array_combine($time_intervals, array_map($callback, $time_intervals));
}
1
  • I am not sure what you mean by i have defined function formatInterval. The code you are showing defines a format_interval() function, and it is not even clear for which class you defined it (since the function is defined as public function format_interval($interval, $granularity = 2, $langcode = NULL)). You cannot surely add it to the class implementing the date.formatter service, without hijacking Drupal core.
    – avpaderno
    Commented Jul 19, 2016 at 11:32
1

There is no default service for date in drupal 8, but it has service for date formatter, you could do that simply with following code,

\Drupal::service('date.formatter')->format($date, 'date_text');

For more formatting check the article

1

The code contains two errors, since Drupal 8

  • doesn't have any date service
  • doesn't define any drupal_map_assoc() function

The equivalent of Drupal 7 format_interval($interval, $granularity = 2, $langcode = NULL) is \Drupal::service('date.formatter')->formatInterval($interval, $granularity = 2, $langcode = NULL), which also works on Drupal 8.0.x.

If you need to write code that given an array like [30, 60] returns an array like [30 => '30 seconds ago', 60 => '1 minute ago'], this is the code I would use.

$time_intervals = [30, 3600, 10800, 21600, 43200, 86400, 172800, 259200, 604800, 1209600, 2592000, 5184000, 7776000];

$options = build_options($time_intervals);

function build_options(array $time_intervals, $granularity = 2, $langcode = NULL) {
  $callback = function ($value) use ($granularity, $langcode) {
    return \Drupal::service('date.formatter')->formatInterval($value, $granularity, $langcode);
  };

  return array_combine($time_intervals, array_map($callback, $time_intervals));
}
3
  • Can you please elaborate your code. Using $time_interval = array(.....); & $options = \Drupal::service('date.formatter')->formatInterval($time_intervals, $granularity = 2, $langcode = NULL); is not working. I am missing something.
    – neha
    Commented Jul 19, 2016 at 11:53
  • I updated the answer with the code I would use.
    – avpaderno
    Commented Jul 19, 2016 at 12:30
  • Thanks it works . Except for few minor changes like $options = $this-> build_options($time_intervals); Updating my working piece of code.
    – neha
    Commented Jul 21, 2016 at 12:20
0

For those who got here by searching how to use formatInterval to override a field value here is how it could be done:

$interval = time() - strtotime('time string loaded from entity');
$interval = \Drupal::service('date.formatter')->formatInterval($interval, 2);
$output = t('Was found @interval ago', array('@interval' => $interval));

The solution above is multilingual friendly, translate the text in User interface translation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.