From a12152309a64b5c3b9ea6a41e8da2baa9971f612 Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Sat, 9 Jun 2012 15:51:03 -0400 Subject: Issue #1495648 by plach: Introduce entity language support. --- includes/common.inc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'includes') diff --git a/includes/common.inc b/includes/common.inc index 8755c94df..6dbb9a265 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -7772,6 +7772,44 @@ function entity_label($entity_type, $entity) { return $label; } +/** + * Returns the language of an entity. + * + * @param $entity_type + * The entity type; e.g., 'node' or 'user'. + * @param $entity + * The entity for which to get the language. + * + * @return + * A valid language code or NULL if the entity has no language support. + */ +function entity_language($entity_type, $entity) { + $info = entity_get_info($entity_type); + + // Invoke the callback to get the language. If there is no callback, try to + // get it from a property of the entity, otherwise NULL. + if (isset($info['language callback']) && function_exists($info['language callback'])) { + $langcode = $info['language callback']($entity_type, $entity); + } + elseif (!empty($info['entity keys']['language']) && isset($entity->{$info['entity keys']['language']})) { + $langcode = $entity->{$info['entity keys']['language']}; + } + else { + // The value returned in D8 would be LANGUAGE_NONE, we cannot use it here to + // preserve backward compatibility. In fact this function has been + // introduced very late in the D7 life cycle, mainly as the proper default + // for field_attach_form(). By returning LANGUAGE_NONE when no language + // information is available, we would introduce a potentially BC-breaking + // API change, since field_attach_form() defaults to the default language + // instead of LANGUAGE_NONE. Moreover this allows us to distinguish between + // entities that have no language specified from ones that do not have + // language support at all. + $langcode = NULL; + } + + return $langcode; +} + /** * Helper function for attaching field API validation to entity forms. */ -- cgit v1.2.3