Translation Of Drupal's Field Collections Entity Type

As the number of Drupal clients to the Lingotek service continues to rise, we have come across many who are using the popular Field Collections module to add structure and functionality to their web content. Lingotek's goal with the Lingotek Translation module for Drupal is to dramatically simplify the process of translating an entire site. So, we have introduced basic support for field-collection translation.

Field Collection Basics

For those less familiar, a field collection is a field type that may be assigned to content types, the same way a text field or radio button may be assigned. Instead of merely containing data, however, they are actual entities within Drupal 7. Because of this, they are “fieldable”, meaning a site administrator can attach as many fields as she wants to the field collection. Field collections can even contain their own nested field collections in addition to any other basic field type, thus providing a very flexible way of maintaining structured content.

Translation without Lingotek

In its current release, the Field Collections module does not support the saving or display of localized content. Without the Lingotek module, you could inject translations directly into fields inside of a field collection, but the Field Collection module by itself will not understand how to display these. The Field Collection module will simply look for the language-neutral field and display it, regardless of the language variable being passed in via the URL, session, or etc. (Note: there are current developments to the Field Collection module to add translation support via the Entity Translation module, which may help to make Lingotek's support for field collections more robust.)

Lingotek enables Localization of Field Collections

As part of the preparatory process for translating a site, the Lingotek Translation module changes all desired language-neutral content types and related fields to be the site's default language. (This tells the Lingotek Translation Management System which language should serve as the source for translation.) Nodes that contain field collections can be included in this change, allowing the field translations underneath each field collection to be found and displayed by Drupal.

To do this, Lingotek saves the underlying fields of the base node in two languages: the site's default language and language-neutral. The reason for the redundant entries for each field is that when the Lingotek module is not enabled, Drupal will look for the language-neutral field entries and display those always; and when the Lingotek module is enabled, Drupal displays content in the site's default language by default. Lingotek's standard process for storing fields is to save them in the site's default language (unless of course the chosen language for the content is overridden on creation). However, at edit time, the Lingotek module will look for field entries in both the default language and language-neutral, to make sure no changes to field collections are missed if the Lingotek module is ever disabled. Creating a language-neutral entry for all fields conversely allows all fields populated while the Lingotek module is enabled to be viewable and editable if the Lingotek module is disabled.

Nested field collections add some complexity to the translation process. If the field is a top-level field collection (meaning it is attached directly to a node, rather than a parent field colleciton), then the entity ID in its field table is a reference to a node ID. If it is a nested field collection, the entity ID in its field table is a reference to the list of field-collection IDs in the 'field_collection_item' table. This makes the node-save process different when looking up languages for the parent entity, as the field_collection_item table does not store some of the same information as the node table, such as base language and whether to translate. So, some testing of a web site's field collections is a good idea for the administrator who is considering using Lingotek for translating their Drupal sites.

In the Works

We continue to refine the ability to translate field collections, and here are the two areas still being worked on:

  1. Support for nested field collections. Field collections may be nested infinitely (within reason), and the way in which field collections look for other related field collections does not have any built-in way for internationalization. So, additions and changes to nested field collections currently are not saved in the language of the base node. (They are simply saved as language-undefined.) We are working on a way to include additional plumbing surrounding field collections so that nested field collections may be translated in the near future.

  2. Support for field collection changes on the “view” page. There are two ways to make additions or changes to a field collection. The traditional way is using the “edit” page of the node containing the field collection. This way is supported by the Lingotek Translation module. The Field Collection module also allows on-the-fly field-collection manipulation directly from the view page. This method for modifying field collections passes through related but different code and so is currently outside the scope of support for Lingotek's field-collection translation.

We look forward to hearing your experiences as you begin translating field collections using the Lingotek Translation module, and would love to hear your feedback.