Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OEL-718: bcl_timeago filter removed due to it was moved to bootstrap #53

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions modules/oe_whitelabel_helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ Enables the [OpenEuropa Multilingual](https://github.com/openeuropa/oe_multiling
The language switcher block is themed out of the box.

### Twig helpers
#### bcl_timeago filter
Filters a timestamp in "time ago" format, result can be something like "8 hours ago".
```
node.getCreatedTime|bcl_timeago
```
#### bcl_footer_links function
Processes oe_corporate_blocks links to make them compatible with BCL formatting.
```
Expand Down
68 changes: 0 additions & 68 deletions modules/oe_whitelabel_helper/src/TwigExtension/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace Drupal\oe_whitelabel_helper\TwigExtension;

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

/**
Expand All @@ -29,15 +27,6 @@ public function __construct(CacheableDependencyInterface $plugin_manager_block)
$this->pluginManagerBlock = $plugin_manager_block;
}

/**
* {@inheritdoc}
*/
public function getFilters(): array {
return [
new TwigFilter('bcl_timeago', [$this, 'bclTimeAgo']),
];
}

/**
* {@inheritdoc}
*/
Expand All @@ -48,63 +37,6 @@ public function getFunctions(): array {
];
}

/**
* Filters a timestamp in "time ago" format.
*
* @param string $timestamp
* Datetime to be parsed.
*
* @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup
* The translated time ago string.
*/
public function bclTimeAgo(string $timestamp): PluralTranslatableMarkup {
$time = \Drupal::time()->getCurrentTime() - $timestamp;
$time_ago = new PluralTranslatableMarkup(0, 'N/A', 'N/A');
$units = [
31536000 => [
'singular' => '@number year ago',
'plural' => '@number years ago',
],
2592000 => [
'singular' => '@number month ago',
'plural' => '@number months ago',
],
604800 => [
'singular' => '@number week ago',
'plural' => '@number weeks ago',
],
86400 => [
'singular' => '@number day ago',
'plural' => '@number days ago',
],
3600 => [
'singular' => '@number hour ago',
'plural' => '@number hours ago',
],
60 => [
'singular' => '@number minute ago',
'plural' => '@number minutes ago',
],
1 => [
'singular' => '@number second ago',
'plural' => '@number seconds ago',
],
];

foreach ($units as $unit => $format) {
if ($time < $unit) {
continue;
}

$number_of_units = floor($time / $unit);
$time_ago = \Drupal::translation()
->formatPlural($number_of_units, $format['singular'], $format['plural'], ['@number' => $number_of_units]);
break;
}

return $time_ago;
}

/**
* Processes footer links to make them compatible with BCL formatting.
*
Expand Down