- Patch #1439692 by clemens.tolboom, plach, dawehner, das-peter, Gábor Hojtsy: Rename field language properties to langcode.

8.0.x
Dries 2012-04-09 14:51:01 -04:00
parent 99d9ca8e44
commit 47556803a8
14 changed files with 307 additions and 252 deletions

View File

@ -265,9 +265,9 @@ class EntityFieldQuery {
* @param $delta_group
* An arbitrary identifier: conditions in the same group must have the same
* $delta_group.
* @param $language_group
* @param $langcode_group
* An arbitrary identifier: conditions in the same group must have the same
* $language_group.
* $langcode_group.
*
* @return EntityFieldQuery
* The called object.
@ -275,8 +275,8 @@ class EntityFieldQuery {
* @see EntityFieldQuery::addFieldCondition
* @see EntityFieldQuery::deleted
*/
public function fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
return $this->addFieldCondition($this->fieldConditions, $field, $column, $value, $operator, $delta_group, $language_group);
public function fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $langcode_group = NULL) {
return $this->addFieldCondition($this->fieldConditions, $field, $column, $value, $operator, $delta_group, $langcode_group);
}
/**
@ -291,9 +291,9 @@ class EntityFieldQuery {
* @param $delta_group
* An arbitrary identifier: conditions in the same group must have the same
* $delta_group.
* @param $language_group
* @param $langcode_group
* An arbitrary identifier: conditions in the same group must have the same
* $language_group.
* $langcode_group.
*
* @return EntityFieldQuery
* The called object.
@ -301,8 +301,8 @@ class EntityFieldQuery {
* @see EntityFieldQuery::addFieldCondition
* @see EntityFieldQuery::deleted
*/
public function fieldLanguageCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
return $this->addFieldCondition($this->fieldMetaConditions, $field, 'language', $value, $operator, $delta_group, $language_group);
public function fieldLanguageCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $langcode_group = NULL) {
return $this->addFieldCondition($this->fieldMetaConditions, $field, 'langcode', $value, $operator, $delta_group, $langcode_group);
}
/**
@ -317,9 +317,9 @@ class EntityFieldQuery {
* @param $delta_group
* An arbitrary identifier: conditions in the same group must have the same
* $delta_group.
* @param $language_group
* @param $langcode_group
* An arbitrary identifier: conditions in the same group must have the same
* $language_group.
* $langcode_group.
*
* @return EntityFieldQuery
* The called object.
@ -327,8 +327,8 @@ class EntityFieldQuery {
* @see EntityFieldQuery::addFieldCondition
* @see EntityFieldQuery::deleted
*/
public function fieldDeltaCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
return $this->addFieldCondition($this->fieldMetaConditions, $field, 'delta', $value, $operator, $delta_group, $language_group);
public function fieldDeltaCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $langcode_group = NULL) {
return $this->addFieldCondition($this->fieldMetaConditions, $field, 'delta', $value, $operator, $delta_group, $langcode_group);
}
/**
@ -367,14 +367,14 @@ class EntityFieldQuery {
* against any combination of deltas. By passing the conditions with the
* same $delta_group it will ensure that only values attached to the same
* delta are matched, and entity 1 would then be excluded from the results.
* @param $language_group
* @param $langcode_group
* An arbitrary identifier: conditions in the same group must have the same
* $language_group.
* $langcode_group.
*
* @return EntityFieldQuery
* The called object.
*/
protected function addFieldCondition(&$conditions, $field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
protected function addFieldCondition(&$conditions, $field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $langcode_group = NULL) {
if (is_scalar($field)) {
$field_definition = field_info_field($field);
if (empty($field_definition)) {
@ -392,7 +392,7 @@ class EntityFieldQuery {
'value' => $value,
'operator' => $operator,
'delta_group' => $delta_group,
'language_group' => $language_group,
'langcode_group' => $langcode_group,
);
}
return $this;

View File

@ -1526,7 +1526,9 @@ class EntityFieldQueryTestCase extends DrupalWebTestCase {
sort($results);
sort($intended_results);
}
$this->assertEqual($results, $intended_results, $message);
if (!$this->assertEqual($results, $intended_results, $message)) {
debug($results, $message);
}
}
catch (Exception $e) {
$this->fail('Exception thrown: '. $e->getMessage());

View File

@ -1459,10 +1459,10 @@ function hook_field_attach_prepare_translation_alter(&$entity, $context) {
/**
* Perform alterations on field_language() values.
*
* This hook is invoked to alter the array of display languages for the given
* entity.
* This hook is invoked to alter the array of display language codes for the
* given entity.
*
* @param $display_language
* @param $display_langcode
* A reference to an array of language codes keyed by field name.
* @param $context
* An associative array containing:
@ -1470,11 +1470,11 @@ function hook_field_attach_prepare_translation_alter(&$entity, $context) {
* - entity: The entity with fields to render.
* - langcode: The language code $entity has to be displayed in.
*/
function hook_field_language_alter(&$display_language, $context) {
function hook_field_language_alter(&$display_langcode, $context) {
// Do not apply core language fallback rules if they are disabled or if Locale
// is not registered as a translation handler.
if (variable_get('locale_field_language_fallback', TRUE) && field_has_translation_handler($context['entity_type'], 'locale')) {
locale_field_language_fallback($display_language, $context['entity'], $context['language']);
locale_field_language_fallback($display_langcode, $context['entity'], $context['langcode']);
}
}
@ -1482,22 +1482,22 @@ function hook_field_language_alter(&$display_language, $context) {
* Alter field_available_languages() values.
*
* This hook is invoked from field_available_languages() to allow modules to
* alter the array of available languages for the given field.
* alter the array of available language codes for the given field.
*
* @param $languages
* @param $langcodes
* A reference to an array of language codes to be made available.
* @param $context
* An associative array containing:
* - entity_type: The type of the entity the field is attached to.
* - field: A field data structure.
*/
function hook_field_available_languages_alter(&$languages, $context) {
// Add an unavailable language.
$languages[] = 'xx';
function hook_field_available_languages_alter(&$langcodes, $context) {
// Add an unavailable language code.
$langcodes[] = 'xx';
// Remove an available language.
$index = array_search('yy', $languages);
unset($languages[$index]);
// Remove an available language code.
$index = array_search('yy', $langcodes);
unset($langcodes[$index]);
}
/**
@ -1712,7 +1712,7 @@ function hook_field_storage_load($entity_type, $entities, $age, $fields, $option
->fields('t')
->condition('entity_type', $entity_type)
->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
->condition('language', field_available_languages($entity_type, $field), 'IN')
->condition('langcode', field_available_languages($entity_type, $field), 'IN')
->orderBy('delta');
if (empty($options['deleted'])) {
@ -1723,11 +1723,11 @@ function hook_field_storage_load($entity_type, $entities, $age, $fields, $option
$delta_count = array();
foreach ($results as $row) {
if (!isset($delta_count[$row->entity_id][$row->language])) {
$delta_count[$row->entity_id][$row->language] = 0;
if (!isset($delta_count[$row->entity_id][$row->langcode])) {
$delta_count[$row->entity_id][$row->langcode] = 0;
}
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field['cardinality']) {
$item = array();
// For each column declared by the field, populate the item
// from the prefixed database column.
@ -1737,8 +1737,8 @@ function hook_field_storage_load($entity_type, $entities, $age, $fields, $option
}
// Add the item to the field values for the entity.
$entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
$delta_count[$row->entity_id][$row->language]++;
$entities[$row->entity_id]->{$field_name}[$row->langcode][] = $item;
$delta_count[$row->entity_id][$row->langcode]++;
}
}
}
@ -1773,39 +1773,39 @@ function hook_field_storage_write($entity_type, $entity, $op, $fields) {
$table_name = _field_sql_storage_tablename($field);
$revision_name = _field_sql_storage_revision_tablename($field);
$all_languages = field_available_languages($entity_type, $field);
$field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
$all_langcodes = field_available_languages($entity_type, $field);
$field_langcodes = array_intersect($all_langcodes, array_keys((array) $entity->$field_name));
// Delete and insert, rather than update, in case a value was added.
if ($op == FIELD_STORAGE_UPDATE) {
// Delete languages present in the incoming $entity->$field_name.
// Delete all languages if $entity->$field_name is empty.
$languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
if ($languages) {
// Delete language codes present in the incoming $entity->$field_name.
// Delete all language codes if $entity->$field_name is empty.
$langcodes = !empty($entity->$field_name) ? $field_langcodes : $all_langcodes;
if ($langcodes) {
db_delete($table_name)
->condition('entity_type', $entity_type)
->condition('entity_id', $id)
->condition('language', $languages, 'IN')
->condition('langcode', $langcodes, 'IN')
->execute();
db_delete($revision_name)
->condition('entity_type', $entity_type)
->condition('entity_id', $id)
->condition('revision_id', $vid)
->condition('language', $languages, 'IN')
->condition('langcode', $langcodes, 'IN')
->execute();
}
}
// Prepare the multi-insert query.
$do_insert = FALSE;
$columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
$columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'langcode');
foreach ($field['columns'] as $column => $attributes) {
$columns[] = _field_sql_storage_columnname($field_name, $column);
}
$query = db_insert($table_name)->fields($columns);
$revision_query = db_insert($revision_name)->fields($columns);
foreach ($field_languages as $langcode) {
foreach ($field_langcodes as $langcode) {
$items = (array) $entity->{$field_name}[$langcode];
$delta_count = 0;
foreach ($items as $delta => $item) {
@ -1817,7 +1817,7 @@ function hook_field_storage_write($entity_type, $entity, $op, $fields) {
'revision_id' => $vid,
'bundle' => $bundle,
'delta' => $delta,
'language' => $langcode,
'langcode' => $langcode,
);
foreach ($field['columns'] as $column => $attributes) {
$record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
@ -1955,7 +1955,7 @@ function hook_field_storage_query($query) {
$sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']);
$query->addCondition($select_query, $sql_field, $condition);
// Add delta / language group conditions.
foreach (array('delta', 'language') as $column) {
foreach (array('delta', 'langcode') as $column) {
if (isset($condition[$column . '_group'])) {
$group_name = $condition[$column . '_group'];
if (!isset($groups[$column][$group_name])) {

View File

@ -174,7 +174,7 @@ const FIELD_STORAGE_INSERT = 'insert';
* - 'deleted': If TRUE, the function will operate on deleted fields
* as well as non-deleted fields. If unset or FALSE, only
* non-deleted fields are operated on.
* - 'language': A language code or an array of language codes keyed by field
* - 'langcode': A language code or an array of language codes keyed by field
* name. It will be used to narrow down to a single value the available
* languages to act on.
*/
@ -183,7 +183,7 @@ function _field_invoke($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $opti
$default_options = array(
'default' => FALSE,
'deleted' => FALSE,
'language' => NULL,
'langcode' => NULL,
);
$options += $default_options;
@ -201,10 +201,10 @@ function _field_invoke($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $opti
$function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op;
if (function_exists($function)) {
// Determine the list of languages to iterate on.
$available_languages = field_available_languages($entity_type, $field);
$languages = _field_language_suggestion($available_languages, $options['language'], $field_name);
$available_langcodes = field_available_languages($entity_type, $field);
$langcodes = _field_language_suggestion($available_langcodes, $options['langcode'], $field_name);
foreach ($languages as $langcode) {
foreach ($langcodes as $langcode) {
$items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
$result = $function($entity_type, $entity, $field, $instance, $langcode, $items, $a, $b);
if (isset($result)) {
@ -268,9 +268,9 @@ function _field_invoke($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $opti
* - 'deleted': If TRUE, the function will operate on deleted fields
* as well as non-deleted fields. If unset or FALSE, only
* non-deleted fields are operated on.
* - 'language': A language code or an array of arrays of language codes keyed
* - 'langcode': A language code or an array of arrays of language codes keyed
* by entity id and field name. It will be used to narrow down to a single
* value the available languages to act on.
* value the available language codes to act on.
*
* @return
* An array of returned values keyed by entity id.
@ -280,7 +280,7 @@ function _field_invoke_multiple($op, $entity_type, $entities, &$a = NULL, &$b =
$default_options = array(
'default' => FALSE,
'deleted' => FALSE,
'language' => NULL,
'langcode' => NULL,
);
$options += $default_options;
$field_info = field_info_field_by_ids();
@ -316,12 +316,12 @@ function _field_invoke_multiple($op, $entity_type, $entities, &$a = NULL, &$b =
}
// Extract the field values into a separate variable, easily accessed
// by hook implementations.
// Unless a language suggestion is provided we iterate on all the
// available languages.
$available_languages = field_available_languages($entity_type, $field);
$language = !empty($options['language'][$id]) ? $options['language'][$id] : $options['language'];
$languages = _field_language_suggestion($available_languages, $language, $field_name);
foreach ($languages as $langcode) {
// Unless a language code suggestion is provided we iterate on all the
// available language codes.
$available_langcodes = field_available_languages($entity_type, $field);
$langcode = !empty($options['langcode'][$id]) ? $options['langcode'][$id] : $options['langcode'];
$langcodes = _field_language_suggestion($available_langcodes, $langcode, $field_name);
foreach ($langcodes as $langcode) {
$grouped_items[$field_id][$langcode][$id] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
// Group the instances and entities corresponding to the current
// field.
@ -564,7 +564,7 @@ function field_attach_form($entity_type, $entity, &$form, &$form_state, $langcod
$form += array('#parents' => array());
// If no language is provided use the default site language.
$options = array('language' => field_valid_language($langcode));
$options = array('langcode' => field_valid_language($langcode));
$form += (array) _field_invoke_default('form', $entity_type, $entity, $form, $form_state, $options);
// Add custom weight handling.
@ -1095,7 +1095,7 @@ function field_attach_delete_revision($entity_type, $entity) {
* is provided the current language is used.
*/
function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL) {
$options = array('language' => array());
$options = array('langcode' => array());
// To ensure hooks are only run once per entity, only process items without
// the _field_view_prepared flag.
@ -1106,9 +1106,9 @@ function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcod
// Add this entity to the items to be prepared.
$prepare[$id] = $entity;
// Determine the actual language to display for each field, given the
// languages available in the field data.
$options['language'][$id] = field_language($entity_type, $entity, NULL, $langcode);
// Determine the actual language code to display for each field, given the
// language codes available in the field data.
$options['langcode'][$id] = field_language($entity_type, $entity, NULL, $langcode);
// Mark this item as prepared.
$entity->_field_view_prepared = TRUE;
@ -1171,10 +1171,10 @@ function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcod
* A renderable array for the field values.
*/
function field_attach_view($entity_type, $entity, $view_mode, $langcode = NULL) {
// Determine the actual language to display for each field, given the
// languages available in the field data.
$display_language = field_language($entity_type, $entity, NULL, $langcode);
$options = array('language' => $display_language);
// Determine the actual language code to display for each field, given the
// language codes available in the field data.
$display_langcode = field_language($entity_type, $entity, NULL, $langcode);
$options = array('langcode' => $display_langcode);
// Invoke field_default_view().
$null = NULL;
@ -1192,7 +1192,7 @@ function field_attach_view($entity_type, $entity, $view_mode, $langcode = NULL)
'entity' => $entity,
'view_mode' => $view_mode,
'display' => $view_mode,
'language' => $langcode,
'langcode' => $langcode,
);
drupal_alter('field_attach_view', $output, $context);
@ -1266,7 +1266,7 @@ function field_attach_preprocess($entity_type, $entity, $element, &$variables) {
* The source language from which translate.
*/
function field_attach_prepare_translation($entity_type, $entity, $langcode, $source_entity, $source_langcode) {
$options = array('language' => $langcode);
$options = array('langcode' => $langcode);
// Copy source field values into the entity to be prepared.
_field_invoke_default('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options);
// Let field types handle their own advanced translation pattern if needed.

View File

@ -316,7 +316,7 @@ function _update_7000_field_delete_instance($field_name, $entity_type, $bundle)
* by the $key parameter.
* @ingroup update-api-7.x-to-8.x
*/
function _update_7000_field_read_fields(array $conditions = array(), $key = 'id') {
function _update_8000_field_read_fields(array $conditions = array(), $key = 'id') {
$fields = array();
$query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
->fields('fc');

View File

@ -867,10 +867,10 @@ function field_view_value($entity_type, $entity, $field_name, $item, $display =
* - weight: (float) The weight to assign to the renderable element.
* Defaults to 0.
* @param $langcode
* (Optional) The language the field values are to be shown in. The site's
* current language fallback logic will be applied no values are available
* for the language. If no language is provided the current language will be
* used.
* (Optional) The language code the field values are to be shown in. The
* site's current language fallback logic will be applied when no values are
* available for given language code. If no language code is provided the
* current language will be used.
* @return
* A renderable array for the field value.
*
@ -887,10 +887,10 @@ function field_view_field($entity_type, $entity, $field_name, $display = array()
// Hook invocations are done through the _field_invoke() functions in
// 'single field' mode, to reuse the language fallback logic.
// Determine the actual language to display for the field, given the
// languages available in the field data.
$display_language = field_language($entity_type, $entity, $field_name, $langcode);
$options = array('field_name' => $field_name, 'language' => $display_language);
// Determine the actual language code to display for the field, given the
// language codes available in the field data.
$display_langcode = field_language($entity_type, $entity, $field_name, $langcode);
$options = array('field_name' => $field_name, 'langcode' => $display_langcode);
$null = NULL;
// Invoke prepare_view steps if needed.

View File

@ -15,8 +15,8 @@
* @code
* $entity->{$field_name}[$langcode][$delta][$column_name]
* @endcode
* Every field can hold a single or multiple value for each language belonging
* to the available languages set:
* Every field can hold a single or multiple value for each language code
* belonging to the available language codes set:
* - For untranslatable fields this set only contains LANGUAGE_NOT_SPECIFIED.
* - For translatable fields this set can contain any language code. By default
* it is the list returned by field_content_languages(), which contains all
@ -24,7 +24,7 @@
* default can be altered by modules implementing
* hook_field_available_languages_alter().
*
* The available languages for a particular field are returned by
* The available language codes for a particular field are returned by
* field_available_languages(). Whether a field is translatable is determined by
* calling field_is_translatable(), which checks the $field['translatable']
* property returned by field_info_field(), and whether there is at least one
@ -33,12 +33,12 @@
* translations.
*
* By default, _field_invoke() and _field_invoke_multiple() are processing a
* field in all available languages, unless they are given a language
* field in all available languages, unless they are given a language code
* suggestion. Based on that suggestion, _field_language_suggestion() determines
* the languages to act on.
*
* Most field_attach_*() functions act on all available languages, except for
* the following:
* Most field_attach_*() functions act on all available language codes, except
* for the following:
* - field_attach_form() only takes a single language code, specifying which
* language the field values will be submitted in.
* - field_attach_view() requires the language the entity will be displayed in.
@ -85,11 +85,11 @@ function field_language_delete() {
}
/**
* Collects the available languages for the given entity type and field.
* Collects the available language codes for the given entity type and field.
*
* If the given field has language support enabled, an array of available
* languages will be returned, otherwise only LANGUAGE_NOT_SPECIFIED will be
* returned. Since the default value for a 'translatable' entity property is
* language codes will be returned, otherwise only LANGUAGE_NOT_SPECIFIED will
* be returned. Since the default value for a 'translatable' entity property is
* FALSE, we ensure that only entities that are able to handle translations
* actually get translatable fields.
*
@ -104,38 +104,39 @@ function field_language_delete() {
function field_available_languages($entity_type, $field) {
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['field_languages'] = &drupal_static(__FUNCTION__);
$drupal_static_fast['field_langcodes'] = &drupal_static(__FUNCTION__);
}
$field_languages = &$drupal_static_fast['field_languages'];
$field_langcodes = &$drupal_static_fast['field_langcodes'];
$field_name = $field['field_name'];
if (!isset($field_languages[$entity_type][$field_name])) {
if (!isset($field_langcodes[$entity_type][$field_name])) {
// If the field has language support enabled we retrieve an (alterable) list
// of enabled languages, otherwise we return just LANGUAGE_NOT_SPECIFIED.
if (field_is_translatable($entity_type, $field)) {
$languages = field_content_languages();
$langcodes = field_content_languages();
// Let other modules alter the available languages.
$context = array('entity_type' => $entity_type, 'field' => $field);
drupal_alter('field_available_languages', $languages, $context);
$field_languages[$entity_type][$field_name] = $languages;
drupal_alter('field_available_languages', $langcodes, $context);
$field_langcodes[$entity_type][$field_name] = $langcodes;
}
else {
$field_languages[$entity_type][$field_name] = array(LANGUAGE_NOT_SPECIFIED);
$field_langcodes[$entity_type][$field_name] = array(LANGUAGE_NOT_SPECIFIED);
}
}
return $field_languages[$entity_type][$field_name];
return $field_langcodes[$entity_type][$field_name];
}
/**
* Process the given language suggestion based on the available languages.
* Process the given language code suggestion based on the available language
* codes.
*
* If a non-empty language suggestion is provided it must appear among the
* available languages, otherwise it will be ignored.
* If a non-empty language code suggestion is provided it must appear among the
* available language codes, otherwise it will be ignored.
*
* @param $available_languages
* @param $available_langcodes
* An array of valid language codes.
* @param $language_suggestion
* @param $langcode_suggestion
* A language code or an array of language codes keyed by field name.
* @param $field_name
* The name of the field being processed.
@ -143,28 +144,28 @@ function field_available_languages($entity_type, $field) {
* @return
* An array of valid language codes.
*/
function _field_language_suggestion($available_languages, $language_suggestion, $field_name) {
function _field_language_suggestion($available_langcodes, $langcode_suggestion, $field_name) {
// Handle possible language suggestions.
if (!empty($language_suggestion)) {
if (!empty($langcode_suggestion)) {
// We might have an array of language suggestions keyed by field name.
if (is_array($language_suggestion) && isset($language_suggestion[$field_name])) {
$language_suggestion = $language_suggestion[$field_name];
if (is_array($langcode_suggestion) && isset($langcode_suggestion[$field_name])) {
$langcode_suggestion = $langcode_suggestion[$field_name];
}
// If we have a language suggestion and the suggested language is available,
// we return only it.
if (in_array($language_suggestion, $available_languages)) {
$available_languages = array($language_suggestion);
// If we have a single language code suggestion and it is available, we just
// return it.
if (in_array($langcode_suggestion, $available_langcodes)) {
$available_langcodes = array($langcode_suggestion);
}
}
return $available_languages;
return $available_langcodes;
}
/**
* Returns available content languages.
* Returns available content language codes.
*
* The languages that may be associated to fields include
* The language codes that may be associated to fields include
* LANGUAGE_NOT_SPECIFIED.
*
* @return
@ -229,9 +230,9 @@ function field_has_translation_handler($entity_type, $handler = NULL) {
/**
* Ensures that a given language code is valid.
*
* Checks whether the given language is one of the enabled languages. Otherwise,
* it returns the current, global language; or the site's default language, if
* the additional parameter $default is TRUE.
* Checks whether the given language code is one of the enabled language codes.
* Otherwise, it returns the current, global language code; or the site's
* default language code, if the additional parameter $default is TRUE.
*
* @param $langcode
* The language code to validate.
@ -251,16 +252,18 @@ function field_valid_language($langcode, $default = TRUE) {
}
/**
* Returns the display language for the fields attached to the given entity.
* Returns the display language code for the fields attached to the given
* entity.
*
* The actual language for each given field is determined based on the requested
* language and the actual data available in the fields themselves.
* The actual language code for each given field is determined based on the
* requested language code and the actual data available in the fields
* themselves.
* If there is no registered translation handler for the given entity type, the
* display language to be used is just LANGUAGE_NOT_SPECIFIED, as no other
* display language code to be used is just LANGUAGE_NOT_SPECIFIED, as no other
* language code is allowed by field_available_languages().
*
* If translation handlers are found, we let modules provide alternative display
* languages for fields not having the requested language available.
* language codes for fields not having the requested language code available.
* Core language fallback rules are provided by locale_field_language_fallback()
* which is called by locale_field_language_alter().
*
@ -270,8 +273,8 @@ function field_valid_language($langcode, $default = TRUE) {
* The entity to be displayed.
* @param $field_name
* (optional) The name of the field to be displayed. Defaults to NULL. If
* no value is specified, the display languages for every field attached to
* the given entity will be returned.
* no value is specified, the display language codes for every field attached
* to the given entity will be returned.
* @param $langcode
* (optional) The language code $entity has to be displayed in. Defaults to
* NULL. If no value is given the current language will be used.
@ -281,38 +284,38 @@ function field_valid_language($langcode, $default = TRUE) {
* keyed by field name otherwise.
*/
function field_language($entity_type, $entity, $field_name = NULL, $langcode = NULL) {
$display_languages = &drupal_static(__FUNCTION__, array());
$display_langcodes = &drupal_static(__FUNCTION__, array());
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
$langcode = field_valid_language($langcode, FALSE);
if (!isset($display_languages[$entity_type][$id][$langcode])) {
$display_language = array();
if (!isset($display_langcodes[$entity_type][$id][$langcode])) {
$display_langcode = array();
// By default display language is set to LANGUAGE_NOT_SPECIFIED if the field
// translation is not available. It is up to translation handlers to
// implement language fallback rules.
foreach (field_info_instances($entity_type, $bundle) as $instance) {
$display_language[$instance['field_name']] = isset($entity->{$instance['field_name']}[$langcode]) ? $langcode : LANGUAGE_NOT_SPECIFIED;
$display_langcode[$instance['field_name']] = isset($entity->{$instance['field_name']}[$langcode]) ? $langcode : LANGUAGE_NOT_SPECIFIED;
}
if (field_has_translation_handler($entity_type)) {
$context = array(
'entity_type' => $entity_type,
'entity' => $entity,
'language' => $langcode,
'langcode' => $langcode,
);
drupal_alter('field_language', $display_language, $context);
drupal_alter('field_language', $display_langcode, $context);
}
$display_languages[$entity_type][$id][$langcode] = $display_language;
$display_langcodes[$entity_type][$id][$langcode] = $display_langcode;
}
$display_language = $display_languages[$entity_type][$id][$langcode];
$display_langcode = $display_langcodes[$entity_type][$id][$langcode];
// Single-field mode.
if (isset($field_name)) {
return isset($display_language[$field_name]) ? $display_language[$field_name] : FALSE;
return isset($display_langcode[$field_name]) ? $display_langcode[$field_name] : FALSE;
}
return $display_language;
return $display_langcode;
}

View File

@ -52,7 +52,7 @@ function _update_7000_field_sql_storage_write($entity_type, $bundle, $entity_id,
'revision_id' => $revision_id,
'bundle' => $bundle,
'delta' => $delta,
'language' => $langcode,
'langcode' => $langcode,
);
foreach ($item as $column => $value) {
$record[_field_sql_storage_columnname($field_name, $column)] = $value;
@ -77,3 +77,52 @@ function _update_7000_field_sql_storage_write($entity_type, $bundle, $entity_id,
$revision_query->execute();
}
}
/**
* Changes field language into langcode.
*/
function field_sql_storage_update_8000(&$sandbox) {
// Prepare updated schema data structures.
$primary_key_data = array (
'entity_type',
'entity_id',
'deleted',
'delta',
'langcode',
);
$primary_key_revision = array (
'entity_type',
'entity_id',
'revision_id',
'deleted',
'delta',
'langcode',
);
$langcode_index = array(
'langcode',
);
$field_langcode = array(
'type' => 'varchar',
'length' => 32,
'not null' => true,
'default' => '',
);
// Retrieve field data.
$fields = _update_8000_field_read_fields(array('storage_type' => 'field_sql_storage'));
// Update schema.
foreach ($fields as $field) {
$data_table = _field_sql_storage_tablename($field);
$revision_table = _field_sql_storage_revision_tablename($field);
$table_info = array($data_table => $primary_key_data, $revision_table => $primary_key_revision);
foreach ($table_info as $table => $primary_key) {
db_drop_primary_key($table);
db_drop_index($table, 'language');
db_change_field($table, 'language', 'langcode', $field_langcode);
db_add_primary_key($table, $primary_key);
db_add_index($table, 'langcode', $langcode_index);
}
}
}

View File

@ -146,13 +146,13 @@ function _field_sql_storage_schema($field) {
'not null' => FALSE,
'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
),
// @todo Consider storing language as integer.
'language' => array(
// @todo Consider storing langcode as integer.
'langcode' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'The language for this data item.',
'description' => 'The language code for this data item.',
),
'delta' => array(
'type' => 'int',
@ -161,14 +161,14 @@ function _field_sql_storage_schema($field) {
'description' => 'The sequence number for this data item, used for multi-value fields',
),
),
'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'),
'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'langcode'),
'indexes' => array(
'entity_type' => array('entity_type'),
'bundle' => array('bundle'),
'deleted' => array('deleted'),
'entity_id' => array('entity_id'),
'revision_id' => array('revision_id'),
'language' => array('language'),
'langcode' => array('langcode'),
),
);
@ -200,7 +200,7 @@ function _field_sql_storage_schema($field) {
// Construct the revision table.
$revision = $current;
$revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})";
$revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language');
$revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'langcode');
$revision['fields']['revision_id']['not null'] = TRUE;
$revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to';
@ -339,7 +339,7 @@ function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fi
->fields('t')
->condition('entity_type', $entity_type)
->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
->condition('language', field_available_languages($entity_type, $field), 'IN')
->condition('langcode', field_available_languages($entity_type, $field), 'IN')
->orderBy('delta');
if (empty($options['deleted'])) {
@ -350,11 +350,11 @@ function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fi
$delta_count = array();
foreach ($results as $row) {
if (!isset($delta_count[$row->entity_id][$row->language])) {
$delta_count[$row->entity_id][$row->language] = 0;
if (!isset($delta_count[$row->entity_id][$row->langcode])) {
$delta_count[$row->entity_id][$row->langcode] = 0;
}
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field['cardinality']) {
$item = array();
// For each column declared by the field, populate the item
// from the prefixed database column.
@ -364,8 +364,8 @@ function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fi
}
// Add the item to the field values for the entity.
$entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
$delta_count[$row->entity_id][$row->language]++;
$entities[$row->entity_id]->{$field_name}[$row->langcode][] = $item;
$delta_count[$row->entity_id][$row->langcode]++;
}
}
}
@ -386,39 +386,39 @@ function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fiel
$table_name = _field_sql_storage_tablename($field);
$revision_name = _field_sql_storage_revision_tablename($field);
$all_languages = field_available_languages($entity_type, $field);
$field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
$all_langcodes = field_available_languages($entity_type, $field);
$field_langcodes = array_intersect($all_langcodes, array_keys((array) $entity->$field_name));
// Delete and insert, rather than update, in case a value was added.
if ($op == FIELD_STORAGE_UPDATE) {
// Delete languages present in the incoming $entity->$field_name.
// Delete all languages if $entity->$field_name is empty.
$languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
if ($languages) {
// Delete language codes present in the incoming $entity->$field_name.
// Delete all language codes if $entity->$field_name is empty.
$langcodes = !empty($entity->$field_name) ? $field_langcodes : $all_langcodes;
if ($langcodes) {
db_delete($table_name)
->condition('entity_type', $entity_type)
->condition('entity_id', $id)
->condition('language', $languages, 'IN')
->condition('langcode', $langcodes, 'IN')
->execute();
db_delete($revision_name)
->condition('entity_type', $entity_type)
->condition('entity_id', $id)
->condition('revision_id', $vid)
->condition('language', $languages, 'IN')
->condition('langcode', $langcodes, 'IN')
->execute();
}
}
// Prepare the multi-insert query.
$do_insert = FALSE;
$columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
$columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'langcode');
foreach ($field['columns'] as $column => $attributes) {
$columns[] = _field_sql_storage_columnname($field_name, $column);
}
$query = db_insert($table_name)->fields($columns);
$revision_query = db_insert($revision_name)->fields($columns);
foreach ($field_languages as $langcode) {
foreach ($field_langcodes as $langcode) {
$items = (array) $entity->{$field_name}[$langcode];
$delta_count = 0;
foreach ($items as $delta => $item) {
@ -430,7 +430,7 @@ function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fiel
'revision_id' => $vid,
'bundle' => $bundle,
'delta' => $delta,
'language' => $langcode,
'langcode' => $langcode,
);
foreach ($field['columns'] as $column => $attributes) {
$record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
@ -626,8 +626,8 @@ function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, Sele
// Add the specified condition.
$sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
$query->addCondition($select_query, $sql_field, $condition);
// Add delta / language group conditions.
foreach (array('delta', 'language') as $column) {
// Add delta / langcode group conditions.
foreach (array('delta', 'langcode') as $column) {
if (isset($condition[$column . '_group'])) {
$group_name = $condition[$column . '_group'];
if (!isset($groups[$column][$group_name])) {

View File

@ -47,7 +47,7 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase {
$eid = 0;
$langcode = LANGUAGE_NOT_SPECIFIED;
$columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $this->field_name . '_value');
$columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'langcode', $this->field_name . '_value');
// Insert data for four revisions to the field revisions table
$query = db_insert($this->revision_table)->fields($columns);
@ -95,15 +95,16 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase {
}
}
// Add a translation in an unavailable language and verify it is not loaded.
// Add a translation in an unavailable language code and verify it is not
// loaded.
$eid = $evid = 1;
$unavailable_language = 'xx';
$unavailable_langcode = 'xx';
$entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
$values = array($entity_type, $eid, $evid, 0, $unavailable_language, mt_rand(1, 127));
$values = array($entity_type, $eid, $evid, 0, $unavailable_langcode, mt_rand(1, 127));
db_insert($this->table)->fields($columns)->values($values)->execute();
db_insert($this->revision_table)->fields($columns)->values($values)->execute();
field_attach_load($entity_type, array($eid => $entity));
$this->assertFalse(array_key_exists($unavailable_language, $entity->{$this->field_name}), 'Field translation in an unavailable language ignored');
$this->assertFalse(array_key_exists($unavailable_langcode, $entity->{$this->field_name}), 'Field translation in an unavailable language ignored');
}
/**
@ -241,10 +242,10 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase {
$this->assertEqual($count, 0, 'NULL field leaves no data in table');
// Add a translation in an unavailable language.
$unavailable_language = 'xx';
$unavailable_langcode = 'xx';
db_insert($this->table)
->fields(array('entity_type', 'bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'language'))
->values(array($entity_type, $this->instance['bundle'], 0, 0, 0, 0, $unavailable_language))
->fields(array('entity_type', 'bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'langcode'))
->values(array($entity_type, $this->instance['bundle'], 0, 0, 0, 0, $unavailable_langcode))
->execute();
$count = db_select($this->table)
->countQuery()
@ -263,7 +264,7 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase {
// Update: Field translation is missing but field is not empty. Translation
// data should survive.
$entity->{$this->field_name}[$unavailable_language] = array(mt_rand(1, 127));
$entity->{$this->field_name}[$unavailable_langcode] = array(mt_rand(1, 127));
unset($entity->{$this->field_name}[$langcode]);
field_attach_update($entity_type, $entity);
$count = db_select($this->table)

View File

@ -2732,21 +2732,21 @@ class FieldTranslationsTestCase extends FieldTestCase {
// Test hook_field_languages() invocation on a translatable field.
variable_set('field_test_field_available_languages_alter', TRUE);
$enabled_languages = field_content_languages();
$available_languages = field_available_languages($this->entity_type, $this->field);
foreach ($available_languages as $delta => $langcode) {
$enabled_langcodes = field_content_languages();
$available_langcodes = field_available_languages($this->entity_type, $this->field);
foreach ($available_langcodes as $delta => $langcode) {
if ($langcode != 'xx' && $langcode != 'en') {
$this->assertTrue(in_array($langcode, $enabled_languages), t('%language is an enabled language.', array('%language' => $langcode)));
$this->assertTrue(in_array($langcode, $enabled_langcodes), t('%language is an enabled language.', array('%language' => $langcode)));
}
}
$this->assertTrue(in_array('xx', $available_languages), t('%language was made available.', array('%language' => 'xx')));
$this->assertFalse(in_array('en', $available_languages), t('%language was made unavailable.', array('%language' => 'en')));
$this->assertTrue(in_array('xx', $available_langcodes), t('%language was made available.', array('%language' => 'xx')));
$this->assertFalse(in_array('en', $available_langcodes), t('%language was made unavailable.', array('%language' => 'en')));
// Test field_available_languages() behavior for untranslatable fields.
$this->field['translatable'] = FALSE;
field_update_field($this->field);
$available_languages = field_available_languages($this->entity_type, $this->field);
$this->assertTrue(count($available_languages) == 1 && $available_languages[0] === LANGUAGE_NOT_SPECIFIED, t('For untranslatable fields only LANGUAGE_NOT_SPECIFIED is available.'));
$available_langcodes = field_available_languages($this->entity_type, $this->field);
$this->assertTrue(count($available_langcodes) == 1 && $available_langcodes[0] === LANGUAGE_NOT_SPECIFIED, t('For untranslatable fields only LANGUAGE_NOT_SPECIFIED is available.'));
}
/**
@ -2762,14 +2762,14 @@ class FieldTranslationsTestCase extends FieldTestCase {
// Populate some extra languages to check if _field_invoke() correctly uses
// the result of field_available_languages().
$values = array();
$extra_languages = mt_rand(1, 4);
$languages = $available_languages = field_available_languages($this->entity_type, $this->field);
for ($i = 0; $i < $extra_languages; ++$i) {
$languages[] = $this->randomName(2);
$extra_langcodes = mt_rand(1, 4);
$langcodes = $available_langcodes = field_available_languages($this->entity_type, $this->field);
for ($i = 0; $i < $extra_langcodes; ++$i) {
$langcodes[] = $this->randomName(2);
}
// For each given language provide some random values.
foreach ($languages as $langcode) {
foreach ($langcodes as $langcode) {
for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {
$values[$langcode][$delta]['value'] = mt_rand(1, 127);
}
@ -2784,7 +2784,7 @@ class FieldTranslationsTestCase extends FieldTestCase {
$this->assertEqual($hash, $result, t('The result for %language is correctly stored.', array('%language' => $langcode)));
}
$this->assertEqual(count($results), count($available_languages), t('No unavailable language has been processed.'));
$this->assertEqual(count($results), count($available_langcodes), t('No unavailable language has been processed.'));
}
/**
@ -2799,23 +2799,23 @@ class FieldTranslationsTestCase extends FieldTestCase {
$entities = array();
$entity_type = 'test_entity';
$entity_count = mt_rand(2, 5);
$available_languages = field_available_languages($this->entity_type, $this->field);
$available_langcodes = field_available_languages($this->entity_type, $this->field);
for ($id = 1; $id <= $entity_count; ++$id) {
$entity = field_test_create_stub_entity($id, $id, $this->instance['bundle']);
$languages = $available_languages;
$langcodes = $available_langcodes;
// Populate some extra languages to check whether _field_invoke()
// correctly uses the result of field_available_languages().
$extra_languages = mt_rand(1, 4);
for ($i = 0; $i < $extra_languages; ++$i) {
$languages[] = $this->randomName(2);
$extra_langcodes = mt_rand(1, 4);
for ($i = 0; $i < $extra_langcodes; ++$i) {
$langcodes[] = $this->randomName(2);
}
// For each given language provide some random values.
$language_count = count($languages);
$language_count = count($langcodes);
for ($i = 0; $i < $language_count; ++$i) {
$langcode = $languages[$i];
$langcode = $langcodes[$i];
// Avoid to populate at least one field translation to check that
// per-entity language suggestions work even when available field values
// are different for each language.
@ -2826,8 +2826,8 @@ class FieldTranslationsTestCase extends FieldTestCase {
}
// Ensure that a language for which there is no field translation is
// used as display language to prepare per-entity language suggestions.
elseif (!isset($display_language)) {
$display_language = $langcode;
elseif (!isset($display_langcode)) {
$display_langcode = $langcode;
}
}
@ -2835,7 +2835,7 @@ class FieldTranslationsTestCase extends FieldTestCase {
$entities[$id] = $entity;
// Store per-entity language suggestions.
$options['language'][$id] = field_language($entity_type, $entity, NULL, $display_language);
$options['langcode'][$id] = field_language($entity_type, $entity, NULL, $display_langcode);
}
$grouped_results = _field_invoke_multiple('test_op_multiple', $entity_type, $entities);
@ -2848,14 +2848,14 @@ class FieldTranslationsTestCase extends FieldTestCase {
$this->assertEqual($hash, $result, t('The result for entity %id/%language is correctly stored.', array('%id' => $id, '%language' => $langcode)));
}
}
$this->assertEqual(count($results), count($available_languages), t('No unavailable language has been processed for entity %id.', array('%id' => $id)));
$this->assertEqual(count($results), count($available_langcodes), t('No unavailable language has been processed for entity %id.', array('%id' => $id)));
}
$null = NULL;
$grouped_results = _field_invoke_multiple('test_op_multiple', $entity_type, $entities, $null, $null, $options);
foreach ($grouped_results as $id => $results) {
foreach ($results as $langcode => $result) {
$this->assertTrue(isset($options['language'][$id]), t('The result language %language for entity %id was correctly suggested (display language: %display_language).', array('%id' => $id, '%language' => $langcode, '%display_language' => $display_language)));
$this->assertTrue(isset($options['langcode'][$id]), t('The result language code %langcode for entity %id was correctly suggested (display language: %display_langcode).', array('%id' => $id, '%langcode' => $langcode, '%display_langcode' => $display_langcode)));
}
}
}
@ -2875,9 +2875,9 @@ class FieldTranslationsTestCase extends FieldTestCase {
$entity_type = 'test_entity';
$entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']);
$field_translations = array();
$available_languages = field_available_languages($entity_type, $this->field);
$this->assertTrue(count($available_languages) > 1, t('Field is translatable.'));
foreach ($available_languages as $langcode) {
$available_langcodes = field_available_languages($entity_type, $this->field);
$this->assertTrue(count($available_langcodes) > 1, t('Field is translatable.'));
foreach ($available_langcodes as $langcode) {
$field_translations[$langcode] = $this->_generateTestFieldValues($this->field['cardinality']);
}
@ -2924,8 +2924,8 @@ class FieldTranslationsTestCase extends FieldTestCase {
$entity = field_test_create_stub_entity(1, 1, $this->instance['bundle']);
$instances = field_info_instances($entity_type, $this->instance['bundle']);
$enabled_languages = field_content_languages();
$languages = array();
$enabled_langcodes = field_content_languages();
$langcodes = array();
// Generate field translations for languages different from the first
// enabled.
@ -2935,48 +2935,48 @@ class FieldTranslationsTestCase extends FieldTestCase {
do {
// Index 0 is reserved for the requested language, this way we ensure
// that no field is actually populated with it.
$langcode = $enabled_languages[mt_rand(1, count($enabled_languages) - 1)];
$langcode = $enabled_langcodes[mt_rand(1, count($enabled_langcodes) - 1)];
}
while (isset($languages[$langcode]));
$languages[$langcode] = TRUE;
while (isset($langcodes[$langcode]));
$langcodes[$langcode] = TRUE;
$entity->{$field_name}[$langcode] = $this->_generateTestFieldValues($field['cardinality']);
}
// Test multiple-fields display languages for untranslatable entities.
field_test_entity_info_translatable($entity_type, FALSE);
drupal_static_reset('field_language');
$requested_language = $enabled_languages[0];
$display_language = field_language($entity_type, $entity, NULL, $requested_language);
$requested_langcode = $enabled_langcodes[0];
$display_langcodes = field_language($entity_type, $entity, NULL, $requested_langcode);
foreach ($instances as $instance) {
$field_name = $instance['field_name'];
$this->assertTrue($display_language[$field_name] == LANGUAGE_NOT_SPECIFIED, t('The display language for field %field_name is %language.', array('%field_name' => $field_name, '%language' => LANGUAGE_NOT_SPECIFIED)));
$this->assertTrue($display_langcodes[$field_name] == LANGUAGE_NOT_SPECIFIED, t('The display language for field %field_name is %language.', array('%field_name' => $field_name, '%language' => LANGUAGE_NOT_SPECIFIED)));
}
// Test multiple-fields display languages for translatable entities.
field_test_entity_info_translatable($entity_type, TRUE);
drupal_static_reset('field_language');
$display_language = field_language($entity_type, $entity, NULL, $requested_language);
$display_langcodes = field_language($entity_type, $entity, NULL, $requested_langcode);
foreach ($instances as $instance) {
$field_name = $instance['field_name'];
$langcode = $display_language[$field_name];
$langcode = $display_langcodes[$field_name];
// As the requested language was not assinged to any field, if the
// returned language is defined for the current field, core fallback rules
// were successfully applied.
$this->assertTrue(isset($entity->{$field_name}[$langcode]) && $langcode != $requested_language, t('The display language for the field %field_name is %language.', array('%field_name' => $field_name, '%language' => $langcode)));
$this->assertTrue(isset($entity->{$field_name}[$langcode]) && $langcode != $requested_langcode, t('The display language for the field %field_name is %language.', array('%field_name' => $field_name, '%language' => $langcode)));
}
// Test single-field display language.
drupal_static_reset('field_language');
$langcode = field_language($entity_type, $entity, $this->field_name, $requested_language);
$this->assertTrue(isset($entity->{$this->field_name}[$langcode]) && $langcode != $requested_language, t('The display language for the (single) field %field_name is %language.', array('%field_name' => $field_name, '%language' => $langcode)));
$langcode = field_language($entity_type, $entity, $this->field_name, $requested_langcode);
$this->assertTrue(isset($entity->{$this->field_name}[$langcode]) && $langcode != $requested_langcode, t('The display language for the (single) field %field_name is %language.', array('%field_name' => $field_name, '%language' => $langcode)));
// Test field_language() basic behavior without language fallback.
variable_set('field_test_language_fallback', FALSE);
$entity->{$this->field_name}[$requested_language] = mt_rand(1, 127);
$entity->{$this->field_name}[$requested_langcode] = mt_rand(1, 127);
drupal_static_reset('field_language');
$display_language = field_language($entity_type, $entity, $this->field_name, $requested_language);
$this->assertEqual($display_language, $requested_language, t('Display language behave correctly when language fallback is disabled'));
$display_langcode = field_language($entity_type, $entity, $this->field_name, $requested_langcode);
$this->assertEqual($display_langcode, $requested_langcode, t('Display language behave correctly when language fallback is disabled'));
}
/**
@ -2990,13 +2990,13 @@ class FieldTranslationsTestCase extends FieldTestCase {
field_test_entity_info_translatable($this->entity_type, TRUE);
$eid = 1;
$entity = field_test_create_stub_entity($eid, $eid, $this->instance['bundle']);
$available_languages = array_flip(field_available_languages($this->entity_type, $this->field));
unset($available_languages[LANGUAGE_NOT_SPECIFIED]);
$available_langcodes = array_flip(field_available_languages($this->entity_type, $this->field));
unset($available_langcodes[LANGUAGE_NOT_SPECIFIED]);
$field_name = $this->field['field_name'];
// Store the field translations.
$entity->is_new = TRUE;
foreach ($available_languages as $langcode => $value) {
foreach ($available_langcodes as $langcode => $value) {
$entity->{$field_name}[$langcode][0]['value'] = $value + 1;
}
field_test_entity_save($entity);
@ -3007,18 +3007,18 @@ class FieldTranslationsTestCase extends FieldTestCase {
$this->drupalPost('test-entity/manage/' . $eid . '/edit', $edit, t('Save'));
// Check translation revisions.
$this->checkTranslationRevisions($eid, $eid, $available_languages);
$this->checkTranslationRevisions($eid, $eid + 1, $available_languages);
$this->checkTranslationRevisions($eid, $eid, $available_langcodes);
$this->checkTranslationRevisions($eid, $eid + 1, $available_langcodes);
}
/**
* Check if the field translation attached to the entity revision identified
* by the passed arguments were correctly stored.
*/
private function checkTranslationRevisions($eid, $evid, $available_languages) {
private function checkTranslationRevisions($eid, $evid, $available_langcodes) {
$field_name = $this->field['field_name'];
$entity = field_test_entity_test_load($eid, $evid);
foreach ($available_languages as $langcode => $value) {
foreach ($available_langcodes as $langcode => $value) {
$passed = isset($entity->{$field_name}[$langcode]) && $entity->{$field_name}[$langcode][0]['value'] == $value + 1;
$this->assertTrue($passed, t('The @language translation for revision @revision was correctly stored', array('@language' => $langcode, '@revision' => $entity->ftvid)));
}

View File

@ -101,22 +101,22 @@ function field_test_field_test_op_multiple($entity_type, $entities, $field, $ins
/**
* Implements hook_field_available_languages_alter().
*/
function field_test_field_available_languages_alter(&$languages, $context) {
function field_test_field_available_languages_alter(&$langcodes, $context) {
if (variable_get('field_test_field_available_languages_alter', FALSE)) {
// Add an unavailable language.
$languages[] = 'xx';
// Remove an available language.
$index = array_search('en', $languages);
unset($languages[$index]);
// Add an unavailable language code.
$langcodes[] = 'xx';
// Remove an available language code.
$index = array_search('en', $langcodes);
unset($langcodes[$index]);
}
}
/**
* Implements hook_field_language_alter().
*/
function field_test_field_language_alter(&$display_language, $context) {
function field_test_field_language_alter(&$display_langcode, $context) {
if (variable_get('field_test_language_fallback', TRUE)) {
locale_field_language_fallback($display_language, $context['entity'], $context['language']);
locale_field_language_fallback($display_langcode, $context['entity'], $context['langcode']);
}
}

View File

@ -94,17 +94,17 @@ function field_test_field_storage_load($entity_type, $entities, $age, $fields, $
foreach ($field_data[$sub_table] as $row) {
if ($row->type == $entity_type && (!$row->deleted || $options['deleted'])) {
if (($load_current && in_array($row->entity_id, $ids)) || (!$load_current && in_array($row->revision_id, $ids))) {
if (in_array($row->language, field_available_languages($entity_type, $field))) {
if (!isset($delta_count[$row->entity_id][$row->language])) {
$delta_count[$row->entity_id][$row->language] = 0;
if (in_array($row->langcode, field_available_languages($entity_type, $field))) {
if (!isset($delta_count[$row->entity_id][$row->langcode])) {
$delta_count[$row->entity_id][$row->langcode] = 0;
}
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field['cardinality']) {
$item = array();
foreach ($field['columns'] as $column => $attributes) {
$item[$column] = $row->{$column};
}
$entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
$delta_count[$row->entity_id][$row->language]++;
$entities[$row->entity_id]->{$field_name}[$row->langcode][] = $item;
$delta_count[$row->entity_id][$row->langcode]++;
}
}
}
@ -126,17 +126,17 @@ function field_test_field_storage_write($entity_type, $entity, $op, $fields) {
$field_name = $field['field_name'];
$field_data = &$data[$field_id];
$all_languages = field_available_languages($entity_type, $field);
$field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
$all_langcodes = field_available_languages($entity_type, $field);
$field_langcodes = array_intersect($all_langcodes, array_keys((array) $entity->$field_name));
// Delete and insert, rather than update, in case a value was added.
if ($op == FIELD_STORAGE_UPDATE) {
// Delete languages present in the incoming $entity->$field_name.
// Delete all languages if $entity->$field_name is empty.
$languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
if ($languages) {
$langcodes = !empty($entity->$field_name) ? $field_langcodes : $all_langcodes;
if ($langcodes) {
foreach ($field_data['current'] as $key => $row) {
if ($row->type == $entity_type && $row->entity_id == $id && in_array($row->language, $languages)) {
if ($row->type == $entity_type && $row->entity_id == $id && in_array($row->langcode, $langcodes)) {
unset($field_data['current'][$key]);
}
}
@ -150,7 +150,7 @@ function field_test_field_storage_write($entity_type, $entity, $op, $fields) {
}
}
foreach ($field_languages as $langcode) {
foreach ($field_langcodes as $langcode) {
$items = (array) $entity->{$field_name}[$langcode];
$delta_count = 0;
foreach ($items as $delta => $item) {
@ -162,7 +162,7 @@ function field_test_field_storage_write($entity_type, $entity, $op, $fields) {
'bundle' => $bundle,
'delta' => $delta,
'deleted' => FALSE,
'language' => $langcode,
'langcode' => $langcode,
);
foreach ($field['columns'] as $column => $attributes) {
$row->{$column} = isset($item[$column]) ? $item[$column] : NULL;

View File

@ -347,11 +347,11 @@ function locale_theme() {
/**
* Implements hook_field_language_alter().
*/
function locale_field_language_alter(&$display_language, $context) {
function locale_field_language_alter(&$display_langcode, $context) {
// Do not apply core language fallback rules if they are disabled or if Locale
// is not registered as a translation handler.
if (variable_get('locale_field_language_fallback', TRUE) && field_has_translation_handler($context['entity_type'], 'locale')) {
locale_field_language_fallback($display_language, $context['entity'], $context['language']);
locale_field_language_fallback($display_langcode, $context['entity'], $context['langcode']);
}
}
@ -366,32 +366,32 @@ function locale_field_language_alter(&$display_language, $context) {
* behavior can be disabled by setting the 'locale_field_language_fallback'
* variable to FALSE.
*
* @param $display_language
* @param $field_langcodes
* A reference to an array of language codes keyed by field name.
* @param $entity
* The entity to be displayed.
* @param $langcode
* The language code $entity has to be displayed in.
*/
function locale_field_language_fallback(&$display_language, $entity, $langcode) {
function locale_field_language_fallback(&$field_langcodes, $entity, $langcode) {
// Lazily init fallback candidates to avoid unnecessary calls.
$fallback_candidates = NULL;
$field_languages = array();
foreach ($display_language as $field_name => $field_language) {
foreach ($field_langcodes as $field_name => $field_langcode) {
// If the requested language is defined for the current field use it,
// otherwise search for a fallback value among the fallback candidates.
if (isset($entity->{$field_name}[$langcode])) {
$display_language[$field_name] = $langcode;
$field_langcodes[$field_name] = $langcode;
}
elseif (!empty($entity->{$field_name})) {
if (!isset($fallback_candidates)) {
require_once DRUPAL_ROOT . '/core/includes/language.inc';
$fallback_candidates = language_fallback_get_candidates();
}
foreach ($fallback_candidates as $fallback_language) {
if (isset($entity->{$field_name}[$fallback_language])) {
$display_language[$field_name] = $fallback_language;
foreach ($fallback_candidates as $fallback_langcode) {
if (isset($entity->{$field_name}[$fallback_langcode])) {
$field_langcodes[$field_name] = $fallback_langcode;
break;
}
}