Issue

In some cases, the date field may output an empty value when using the timeago date formatter, causing two DOM elements with class="timeago." The second has a text node with a single whitespace, which is parsed by the jQuery function and outputs a duplicate time ago statement.

Proposed solution

Sanity check in JS for empty / whitespace only DOM nodes with class "timeago." I'm not sure how to remove the second empty DOM node on the Drupal output side, but that would be a better solution.

Comments

angrytoast’s picture

Status: Active » Needs review
StatusFileSize
new571 bytes

Patch attached:

icecreamyou’s picture

In some cases, the date field may output an empty value

What are these cases?

causing two DOM elements with class="timeago"

Why does that happen?

The second has a text node with a single whitespace, which is parsed by the jQuery function and outputs a duplicate time ago statement.

Is that the expected behavior for the Timeago library?

This sounds like something that should be fixed either server-side or in the Timeago library itself (or both).

narkoff’s picture

I was having a very similar issue where I was getting duplicate timeago dates when the Views formatter was set to 'Date and time' and selecting a custom timeago date format.

Conversely, timeago worked properly (returning single dates) if I select the 'Timeago' formatter in Views. However, this formatter would not work for future dates. The returned date was '0 sec'. I could only get future dates to work using the' Date and time' formatter.

This patch solved the duplicate dates issue using the 'Date and time' formatter. Thanks!

fau’s picture

Issue summary: View changes

There is a problem with "Date and time" formatter from Date API. What it does is it limits our custom date format to date, time and timezone, and then formats them individually (see date.module: date_formatter_process). Later they get reused in various contexts.

For example a date field with display "start and end date" gets rendered like this: $start_formatted . ' - ' . $end_formatted_time . ' ' $end_formatted_timezone and the output is Thursday, April 9, 2015 - 19:00 CEST to 21:00 CEST. (see date.theme: theme_date_display_combination)

Unfortunately each of these variables gets formatted separately, so we end up with three class="timeago" html wrappers.

If our date format doesn't have timezone format characters, $end_formatted_timezone won't be rendered only if all custom characters (besides punctuation) in date format are escaped, like this:

Long: \<\s\p\a\n \c\l\a\s\s\=\\\"\t\i\m\e\a\g\o\\\" \t\i\t\l\e\=\\\"c"\>l, j F Y - H:i\<\/\s\p\a\n\>
Medium: \<\s\p\a\n \c\l\a\s\s\=\\\"\t\i\m\e\a\g\o\\\" \t\i\t\l\e\=\\\"c"\>D, d/m/Y - H:i\<\/\s\p\a\n\>
Short: \<\s\p\a\n \c\l\a\s\s\=\\\"\t\i\m\e\a\g\o\\\" \t\i\t\l\e\=\\\"c"\>d/m/Y - H:i\<\/\s\p\a\n\>

These also work with php date() function and could replace module's defaults.
Notice that last " character must be unescaped. I think this is due to a bug in date_limit_format (but I don't understand their preg_replace pattern).

Of course "Start and end date" display will still output rubbish with timeago date type, so the resolution of this bug should be that "Date and time" formatter isn't supported. However with the aforementioned date format strings you can use timeago with it when there is only a single date displayed. Original patch gets the job done too.

kopeboy’s picture

I think you have put too many escapes.

This worked for me:
\<\s\p\a\n \c\l\a\s\s\=\"\t\i\m\e\a\g\o\" \t\i\t\l\e\=\"c"\>l, j F Y - H:i\<\/\s\p\a\n\>

So the steps to do to make timeago work, without patches as of now, with "Date and time" format for a field (in Manage Display or a View) are:

  1. Go to /admin/config/regional/date-time/formats/add
  2. Write the format you like escaping everything, like shown above (you can edit the I, j F Y - U:i part) and save
  3. Go to /admin/config/regional/date-time and select the format you created for the Date type you want (for example the "ISO 8601")
  4. Use that format for your field in Manage Display or a View

I think you could rewrite the module provided formats to use the escaped notation so they would still work out of the box if a site has Date module installed (half a million at least...)

kopeboy’s picture

After 6 years the problem is still present:

enabling this module and using any provded date type, will crash the admin manage display of every date field with format Date and time

Please fix them.

atul4drupal’s picture

Status: Needs review » Needs work