Flag and i18n
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
This documentation needs review. See "Help improve this page" in the sidebar.
This page lists some issues with Flag's i18n support.
⚠️ Note for Drupal 11 (and recent versions)
This page describes procedures intended for Drupal 6.x and 7.x and is no longer up to date. On Drupal 11, translating Flag module configuration strings (such as "Flag link text" and "Unflag link text") no longer requires additional modules like Views PHP or Views Custom Field, nor custom PHP snippets.
To translate Flag configuration fields:
- Make sure the core modules Configuration Translation (
config_translation) and Language are enabled. - Go to Configuration > Regional and language > Configuration translation (
/admin/config/regional/config-translation), or directly to the flag management page: Structure > Flags (/admin/structure/flags). - Next to the flag you want to translate, open the dropdown menu and select Translate.
- Enter the translations for the strings exposed by the flag (link text, etc.).
The sections below are kept only as historical reference for Drupal 6/7 installations and do not apply to Drupal 11.
"Flag translations of content as a group"
If you install and enable Translation helpers module, the setting "Flag translations of content as a group" becomes available in each node flag setting page. When the setting is chosen, then flagging or unflagging a node is carried out on the translation source node instead. For example, if you have a French translation of an English node, and the English node is considered the "source", then the flag links displayed beneath both nodes will flag or unflag the English node.
Usually you don't have to be aware of this fact when you write PHP snippets. I.e., the $flag->flag() and $flag->is_flagged() and flag_create_link() methods described in the API section will work as "expected". These methods accept the $nid of the translated node and they internally convert it to the $nid of the source.
Views
However, the "Flag translations of content as a group" magic described above doesn't extend to Views. The flag relationships and flag fields operate on the immediate node and the source node is ignored. This means that some steps you'll have to carry out explicitly.
Displaying the correct flag link
When you format your view as a "Table" or "Fields" (as opposed to node's "Full" or "Teaser") you'll note that the "flag this!" and "unflag link!" links don't reflect the state of the node's source. Probably the easiest workaround is this:
For Drupal 6.x:
- Install the Views Custom Field module.
- Add the "Customfield: PHP code" field to your view.
- Into the field's "Value:" setting type:
// Replace 'bookmarks' with the machine-name of your flag. print flag_create_link('bookmarks', $data->nid);
For Drupal 7.x:
- Install the Views PHP module.
- Add the "Global: PHP code" field to your view.
- Into the field's "Output:" setting type:
// Replace 'bookmarks' with the machine-name of your flag. print flag_create_link('bookmarks', $data->nid);
Filtering the nodes to the flagged ones only
It seems there are two approaches:
- Start with the translations. Then bring in the "Node translation: Source translation" relationship to have access to the source node, and filter on it.
- Start with a view of the source nodes, filter them, then bring in the "Node translation: Translations" relationship to show the translated nodes. Here is a step-by-step example of how to do this.
(@todo: It's possible that we can use the i18n module's query rewriting to do all or some of this work. Investigate.)
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion