diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index 1b65f91..899ae9f 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -61,6 +61,13 @@ class EntityNG extends Entity { protected $bcEntity; /** + * Local cache for the entity language. + * + * @var \Drupal\Core\Language\Language + */ + protected $language; + + /** * Local cache for field definitions. * * @see EntityNG::getPropertyDefinitions() @@ -326,15 +333,30 @@ public function isEmpty() { * Implements \Drupal\Core\TypedData\TranslatableInterface::language(). */ public function language() { - // Get the language code if the property exists. - if ($this->getPropertyDefinition('langcode')) { - $language = $this->get('langcode')->language; + // Keep a local cache of the language object and clear it if the langcode + // gets changed, see EntityNG::onChange(). + if (!isset($this->language)) { + // Get the language code if the property exists. + if ($this->getPropertyDefinition('langcode')) { + $this->language = $this->get('langcode')->language; + } + if (empty($this->language)) { + // Make sure we return a proper language object. + $this->language = new Language(array('langcode' => Language::LANGCODE_NOT_SPECIFIED)); + } } - if (empty($language)) { - // Make sure we return a proper language object. - $language = new Language(array('langcode' => Language::LANGCODE_NOT_SPECIFIED)); + return $this->language; + } + + /** + * {@inheritdoc} + */ + public function onChange($property_name) { + if ($property_name == 'langcode') { + // Avoid using unset as this unnecessarily triggers magic methods later + // on. + $this->language = NULL; } - return $language; } /** diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php index 09c7d7d..5aeea36 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php @@ -179,6 +179,7 @@ public function invokeFieldMethod($method, EntityInterface $entity) { */ public function invokeFieldMethodMultiple($method, array $entities, $langcode) { $entities_by_bundle = array(); + $typed_data = \Drupal::typedData(); foreach ($entities as $id => $entity) { $entities_by_bundle[$entity->bundle()][$id] = $entity; } @@ -192,11 +193,11 @@ public function invokeFieldMethodMultiple($method, array $entities, $langcode) { foreach ($bundle_entities as $id => $entity) { // @todo Remove the condition and the second code branch when all core // entity types are converted. - if ($entity->getNGEntity() instanceof EntityNG) { + if ($entity instanceof EntityNG || $entity instanceof EntityBCDecorator) { // @todo $entity->getTranslation()->get($name) sometimes fails, // because Entity\Translation::getPropertyDefinitions() is empty() ?? try { - $entities_items[$id] = $entity->getNGEntity()->getTranslation($langcode)->get($field_name); + $entities_items[$id] = $entity->getTranslation($langcode)->get($field_name); } catch (\InvalidArgumentException $e) { break; @@ -210,12 +211,12 @@ public function invokeFieldMethodMultiple($method, array $entities, $langcode) { // @todo Exception : this calls setValue(), tries to set the // 'formatted' property. For now, this is worked around by // commenting out the Exception in TextProcessed::setValue(). - $entities_items[$id] = \Drupal::typedData()->create($definition, $itemsBC, $field_name, $entity); + $entities_items[$id] = $typed_data->create($definition, $itemsBC, $field_name, $entity); } } } - $type_definition = \Drupal::typedData()->getDefinition($definition['type']); + $type_definition = $typed_data->getDefinition($definition['type']); $type_definition['class']::$method($entities_items); // @todo Remove when all core entity types are converted.