summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorDavid Rothstein <drothstein@gmail.com>2012-06-09 15:51:03 -0400
committerDavid Rothstein <drothstein@gmail.com>2012-06-09 15:51:03 -0400
commita12152309a64b5c3b9ea6a41e8da2baa9971f612 (patch)
treea6f27f9eced00ca14d8510be57e4d2e7588d968a /includes
parent4a60f552b4e03916c76bbc7aaea5bc3fa503e85d (diff)
downloadbrdo-a12152309a64b5c3b9ea6a41e8da2baa9971f612.tar.gz
brdo-a12152309a64b5c3b9ea6a41e8da2baa9971f612.tar.bz2
Issue #1495648 by plach: Introduce entity language support.
Diffstat (limited to 'includes')
-rw-r--r--includes/common.inc38
1 files changed, 38 insertions, 0 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 8755c94df..6dbb9a265 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -7773,6 +7773,44 @@ function entity_label($entity_type, $entity) {
}
/**
+ * 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.
*/
function entity_form_field_validate($entity_type, $form, &$form_state) {