| 1 |
<?php
|
| 2 |
// $Id: field.multilingual.inc,v 1.3 2009/10/16 15:47:46 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Multilingual field API helper functions.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_multilingual_settings_changed().
|
| 11 |
*/
|
| 12 |
function field_multilingual_settings_changed() {
|
| 13 |
field_info_cache_clear();
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Collect the available languages for the given entity type and field.
|
| 18 |
*
|
| 19 |
* If an entity has a translation handler and the given field is translatable,
|
| 20 |
* a (not necessarily strict) subset of the current enabled languages will be
|
| 21 |
* returned, otherwise only FIELD_LANGUAGE_NONE will be returned. Since the
|
| 22 |
* default value for a 'translatable' entity property is FALSE, we ensure that
|
| 23 |
* only entities that are able to handle translations actually get translatable
|
| 24 |
* fields.
|
| 25 |
*
|
| 26 |
* @param $obj_type
|
| 27 |
* The type of the entity the field is attached to, e.g. 'node' or 'user'.
|
| 28 |
* @param $field
|
| 29 |
* A field structure.
|
| 30 |
* @param $suggested_languages
|
| 31 |
* An array of language preferences which will be intersected with the enabled
|
| 32 |
* languages.
|
| 33 |
* @return
|
| 34 |
* An array of valid language codes.
|
| 35 |
*/
|
| 36 |
function field_multilingual_available_languages($obj_type, $field, $suggested_languages = NULL) {
|
| 37 |
$field_languages = &drupal_static(__FUNCTION__, array());
|
| 38 |
$field_name = $field['field_name'];
|
| 39 |
|
| 40 |
if (!isset($field_languages[$field_name]) || !empty($suggested_languages)) {
|
| 41 |
$translation_handlers = field_multilingual_check_translation_handlers($obj_type);
|
| 42 |
|
| 43 |
if ($translation_handlers && $field['translatable']) {
|
| 44 |
// The returned languages are a subset of the intersection of enabled ones
|
| 45 |
// and suggested ones.
|
| 46 |
$available_languages = field_multilingual_content_languages();
|
| 47 |
$languages = !empty($suggested_languages) ? $available_languages = array_intersect($available_languages, $suggested_languages) : $available_languages;
|
| 48 |
|
| 49 |
foreach (module_implements('field_languages') as $module) {
|
| 50 |
$function = $module . '_field_languages';
|
| 51 |
$function($obj_type, $field, $languages);
|
| 52 |
}
|
| 53 |
// Accept only available languages.
|
| 54 |
$result = array_values(array_intersect($available_languages, $languages));
|
| 55 |
// Do not cache suggested values as they might alter the general result.
|
| 56 |
if (empty($suggested_languages)) {
|
| 57 |
$field_languages[$field_name] = $result;
|
| 58 |
}
|
| 59 |
}
|
| 60 |
else {
|
| 61 |
$result = $field_languages[$field_name] = array(FIELD_LANGUAGE_NONE);
|
| 62 |
}
|
| 63 |
}
|
| 64 |
else {
|
| 65 |
$result = $field_languages[$field_name];
|
| 66 |
}
|
| 67 |
|
| 68 |
return $result;
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Return available content languages.
|
| 73 |
*
|
| 74 |
* The languages that may be associated to fields include FIELD_LANGAUGE_NONE.
|
| 75 |
*
|
| 76 |
* @return
|
| 77 |
* An array of language codes.
|
| 78 |
*/
|
| 79 |
function field_multilingual_content_languages() {
|
| 80 |
return array_keys(language_list() + array(FIELD_LANGUAGE_NONE => NULL));
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
| 84 |
* Check if a module is registered as a translation handler for a given entity.
|
| 85 |
*
|
| 86 |
* If no handler is passed, simply check if there is any translation handler
|
| 87 |
* enabled for the given entity type.
|
| 88 |
*
|
| 89 |
* @param $obj_type
|
| 90 |
* The type of the entity whose fields are to be translated.
|
| 91 |
* @param $handler
|
| 92 |
* The name of the handler to be checked.
|
| 93 |
*
|
| 94 |
* @return
|
| 95 |
* TRUE, if the handler is allowed to manage field translations.
|
| 96 |
*/
|
| 97 |
function field_multilingual_check_translation_handlers($obj_type, $handler = NULL) {
|
| 98 |
$obj_info = entity_get_info($obj_type);
|
| 99 |
|
| 100 |
if (isset($handler)) {
|
| 101 |
return isset($obj_info['translation'][$handler]) && !empty($obj_info['translation'][$handler]);
|
| 102 |
}
|
| 103 |
elseif (isset($obj_info['translation'])) {
|
| 104 |
foreach ($obj_info['translation'] as $handler_info) {
|
| 105 |
// The translation handler must use a non-empty data structure.
|
| 106 |
if (!empty($handler_info)) {
|
| 107 |
return TRUE;
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
return FALSE;
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Helper function to ensure that a given language code is valid.
|
| 117 |
*
|
| 118 |
* Checks whether the given language is one of the enabled languages. Otherwise,
|
| 119 |
* it returns the current, global language; or the site's default language, if
|
| 120 |
* the additional parameter $default is TRUE.
|
| 121 |
*
|
| 122 |
* @param $langcode
|
| 123 |
* The language code to validate.
|
| 124 |
* @param $default
|
| 125 |
* Whether to return the default language code or the current language code in
|
| 126 |
* case $langcode is invalid.
|
| 127 |
* @return
|
| 128 |
* A valid language code.
|
| 129 |
*/
|
| 130 |
function field_multilingual_valid_language($langcode, $default = TRUE) {
|
| 131 |
$enabled_languages = field_multilingual_content_languages();
|
| 132 |
if (in_array($langcode, $enabled_languages)) {
|
| 133 |
return $langcode;
|
| 134 |
}
|
| 135 |
// @todo Currently, node language neutral code is an empty string. Node passes
|
| 136 |
// $node->language as language parameter to field_attach_form(). We might
|
| 137 |
// want to unify the two "language neutral" language codes.
|
| 138 |
if ($langcode === '') {
|
| 139 |
return FIELD_LANGUAGE_NONE;
|
| 140 |
}
|
| 141 |
global $language;
|
| 142 |
$langcode = $default ? language_default('language') : $language->language;
|
| 143 |
if (in_array($langcode, $enabled_languages)) {
|
| 144 |
return $langcode;
|
| 145 |
}
|
| 146 |
// @todo Throw a more specific exception.
|
| 147 |
throw new FieldException('No valid content language could be found.');
|
| 148 |
}
|