Phéna Proxima

Dispatches from the dimension of demented Drupalists. Also, assorted bullshit.

May 3, 2017

Lightning's Oxford Comma Helper

I added a useful function to the Lightning distribution of Drupal 8! Its purpose is to apply the Oxford comma (with the final conjunction) to an array of strings, returning a single nicely formatted string.

I did this because I don’t enjoy seeing this kind of thing littered throughout Drupal:

The following file types are accepted: gif png jpg txt pdf doc

It gets the point across, and clearly, but it feels so…careless to me. A little polish wouldn’t hurt. Now, if you’re using Lightning, you can generate the string like so:

use Drupal\lightning_core\Element;

echo 'The following file types are accepted: ' . Element::oxford(['gif', 'png', 'jpg', 'txt', 'pdf', 'doc']);

This will produce “The following file types are accepted: gif, png, jpg, txt, pdf, and doc”. Much nicer! In fact, this opens the door to smoother phrasing in general:

Element::oxford(['gif', 'png', 'jpg']) . ' files are acceptable.';

This will produce the even nicer “gif, jpg, and png files are acceptable.” Yowza!

Element::oxford() will default to using ‘and’ as the final conjunction, but you can override that:

Element::oxford(['apples', 'oranges', 'mangoes'], 'or');

This will produce “apples, oranges, or mangoes”.

The function is smart enough to not apply the Oxford comma if there are fewer than three items. So this:

Element::oxford(['foo', 'bar']);

…will produce “foo and bar”.

I think you get the idea. Go forth, Lightning users, and generate smoother, more grammatically correct lists of items!

tags: Drupal 8 - Grammar - Pedantry