Skip to main content

Display lists naturally with the In Other Words module for Drupal

It is common for a Drupal site to list multiple items. It could be several authors of a single article, the days a recreation center is open, or the flavors an ice cream parlor serves. Clean, structured data is a strong point of Drupal, but the display of that structured content is limited out of the box. That is why DevCollaborative partnered with the Agaric Tech Collective to complete a stable release of In Other Words, a Drupal module that gives site builders the power to configure precise and natural ways to display lists of items.

Punctuating Lists

If an article has multiple authors (using a user reference field or a content reference field), Drupal core displays each on their own line.

Like this ugly, rigid presentation:

A screenshot shows whitespace surrounding left-aligned text, with a bold label "Author(s):" in a left-hand column and in a right-hand column "Clayton Dewey" on one line, "Ben Melançon", on the next line, and "David Valdez" on a third line.

Those could be separated by a comma using CSS. For example, "By Clayton Dewey, Ben Melançon, David Valdez."

A more natural display, though, would be "By Clayton Dewey, Ben Melançon, and David Valdez." If, instead of three people, two people wrote the article (as is the case), we would want to display "By Clayton Dewey and Ben Melançon."

Before, a sitebuilder would have to abandon structured content for a text field which an editor would use instead. In other words, instead of using a reference field to display the authors (even if present on the edit form), the sitebuilder would add a regular text field for the content editor to write out the list in a more human-friendly way and that is what would be output, or the content editor would write the byline into the body field.

Now, displaying lists of structured content naturally is possible with the In Other Words module.

(More featureful listings are also possible with Twig templates, but that does not empower sitebuilders— it bogs down themers!)

For text list fields and entity reference fields, In Other Words provides the In Other Words: List field formatter. Its settings page looks like this:

A screenshot of the Format configuration for a field set to In other words: List.  The options are what are described in this post, such as "Join symbol or separator between list items", "Final join word", "Connect final items with a series comma where applicable", "Text before", and "Text after" with some additional detail, as well as the option to "Link label to the referenced entity" is checked.  It ends with the text "Want more options? Open issues in the issue queue" and buttons to Update or Cancel.

(Technically, there is one formatter each for text lists and for entity references, but all the options which In Other Words adds are the same for both.)

We can configure a separator between list items, a final join word, and whether to connect the final items with a comma (or semicolon or any other punctuation we can imagine for our list).

In all its glory in context:

A screenshot shows the headline "Display Lists Naturally with the In Other Words Drupal Module", some whitespace, and then as a single line "By Clayton Dewey, Ben Melançon, and David Valdez".  Below that after another couple lines of whitespace one line of regular text beginning the article is visible.

We could also choose not to connect the final items with a comma, if we were ignoramuses unaware of the Oxford comma. In this case for our example we would see "By Clayton Dewey, Ben Melançon and David Valdez." We are aware, however, so we would enable the series comma option in the In Other Words formatter configuration.

Editor's note: The extreme partisanship in favor of the serial comma is all that Ben contributed to this blog post, but this same unreasoning fervor contributed to starting the module in the first place and creating a PHP package for making lists using the Oxford comma, which the module relies upon.

If we want text before or after, that is also an option. In this case, preceding the authors with "By " is a nice way to denote attribution.

It is worth noting, though, that In Other Words module can count! To display different preceding or following text if only one item is present, put the plural version first and then a | (pipe symbol) and the singular version. We could have configured the field display to hide the "Author(s)" label and configured the field format to have Text before of "Authors: |Author: " if we wanted to keep a more regimented style while including the nicety of precise labels. This feature can also be used for subject-verb agreement, "Jim and Steph are the authors" and "Steph is the author" could be achieved by configuring the Text after to be " are the authors| is the author". (This is a place where we could improve how intuitive the interface is, for sure.)

Shortening Sequential Lists

In Other Words can also shorten sequential lists by skipping over the items in the middle of the sequence. For example, if the following full list is available:

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

And a content author selects the following from them:

Monday, Tuesday, Wednesday, Thursday

The In other words: Sequential terms formatter can be configured to interpret this and output:

Monday through Thursday.

It even accounts for breaks between sequential items. For example:

Monday through Thursday and Saturday.

And if you want to get real fancy, you can group terms together under a single label, such as interpreting Saturdays and Sundays as "the weekend".

Finally, you can have In Other Words output a single phrase if all items are selected. For example, "All Days".

Do so by using the In Other Words: Sequential terms field formatter, which supports term reference fields.

A screenshot of a field's Format settings for In other words: Sequential terms.  The options are what are described in this post, such as "Connecting word", "Minimum number of items in a sequence before removing middle items", "All items text", and "Treat as one label" as well as all the options previously described for In other words: List (Join symbol, Final join word, surrounding text, etc).  It ends with "Want more options? Open issues in the issue queue" and buttons to Update or Cancel.

This same functionality is theoretically possible for other list fields, but In Other Words does not currently support summarizing text and numeric lists. We have opened a feature request to do so, but do not currently have the need ourselves, so we welcome your involvement. Feel free to open issues about things you think would make the module better, too!

A final note. We went through considerable effort to allow In Other Words' conjoining symbols and words, as well as surrounding text, to appear immediately before or after the items in a list with no whitespace. (Note: This does not work when Twig debugging is enabled.) This means you can have information presented as a sentence that ends with a period, "Open Tuesday–Friday." It also means that if you want spacing you need to ensure you add it as part of your join symbol or separator, your final join word, and your before and after text. This is a common gotcha in configuring In Other Words field formatters that you, perhaps, can now avoid!

Conclusion

With the In Other Words module, it is now possible to keep your structured content and display it naturally. The logic of how a list should display in various scenarios is boiled down to an intuitive user interface for site builders, with no coding required. Try it for yourself! Get the module at drupal.org/project/inotherwords.

A large lower case letter i surrounded by tiny letters, that would spell out "capitalism" if they weren't crushed and falling over from the first i.

"Word as Image" by Ji Lee is licensed under CC BY-NC 4.0.

Comments

Add new comment

The content of this field is kept private and will not be shown publicly.

Markdown

The comment language code.
CAPTCHA Please help us focus on people and not spambots by answering this question.