diff --git a/core/modules/entity/lib/Drupal/entity/Entity.php b/core/modules/entity/lib/Drupal/entity/Entity.php index 8e69e11..7cc6ae4 100644 --- a/core/modules/entity/lib/Drupal/entity/Entity.php +++ b/core/modules/entity/lib/Drupal/entity/Entity.php @@ -184,8 +184,9 @@ class Entity implements EntityInterface { $entity_info = $this->entityInfo(); if ($entity_info['fieldable'] && field_info_instance($this->entityType, $property_name, $this->bundle())) { $field = field_info_field($property_name); - // We call getFieldLangcode with strict = FALSE, so if we request a field - // with a invalid language we don't throw an exception. + // Since we are getting and not setting, we call getFieldLangcode with + // strict = FALSE, so if we request a field with a invalid language we + // don't throw an exception. $langcode = $this->getFieldLangcode($field, $langcode, FALSE); return isset($this->{$property_name}[$langcode]) ? $this->{$property_name}[$langcode] : NULL; } @@ -216,7 +217,28 @@ class Entity implements EntityInterface { } /** - * Determines the language code to use for accessing a field value in a certain language. + * Determine the language code for accessing a field value. + * + * The language code to use for accessing a field value in a certain language + * is determined to be the langcode if the entity is language-specific. This + * function handles the special cases of not translateable fields and + * languages that are not specified. + * + * @param $field + * Field the language code is being determined for. + * @param $langcode + * The language code attempting to be applied to the field. + * @param $strict + * Optional flag so that we can be differently strict for invalid langcodes + * when setting and getting field values. If the field is not translatable + * and the langcode is anything other than LANGUAGE_NOT_SPECIFIED, throw + * and exception if $strict is TRUE. When $strict is FALSE, NULL is returned + * instead of the exception being thrown. For example, the EntityInterface + * set() uses $strict TRUE, and the EntityInterface get() uses FALSE. + * + * @return + * The langcode if appropriate, LANGUAGE_NOT_SPECIFIED as in the case of a + * field shared among all language version of an entity, or NULL. */ protected function getFieldLangcode($field, $langcode = NULL, $strict = TRUE) { // Only apply the given langcode if the entity is language-specific. @@ -227,17 +249,18 @@ class Entity implements EntityInterface { return isset($langcode) ? $langcode : $default_language->langcode; } else { + // Field was non-translatable but the attempted langcode was something + // other than LANGUAGE_NOT_SPECIFIED. if ($langcode != NULL && $langcode != LANGUAGE_NOT_SPECIFIED) { // Only throw Exception in strict mode. if ($strict) { - $field_name = $field['field_name']; - throw new EntityStorageException(format_string("Field @field_name is not translatable and does only work with langcode = LANGUAGE_NOT_SPECIFIED", array('@field_name' => $field_name))); + throw new EntityStorageException(format_string("Field @field_name is not translatable and only works with langcode = LANGUAGE_NOT_SPECIFIED", array('@field_name' => $field['field_name']))); } else { return NULL; } } - // For non-translatable the langcode is LANGUAGE_NOT_SPECIFIED. + // Field was non-translatable and the langcode was LANGUAGE_NOT_SPECIFIED. return LANGUAGE_NOT_SPECIFIED; } }