X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Flib%2FDrupal%2FCore%2FEntity%2FContentEntityBase.php;fp=web%2Fcore%2Flib%2FDrupal%2FCore%2FEntity%2FContentEntityBase.php;h=2bf1372b61953b37b98ac6e985ba64dca495be9f;hp=6b999c6977f5f2a0252387b3f8fac7c535579ced;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 6b999c697..2bf1372b6 100644 --- a/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -2,10 +2,9 @@ namespace Drupal\Core\Entity; -use Drupal\Component\Utility\SafeMarkup; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Entity\Plugin\DataType\EntityReference; use Drupal\Core\Field\BaseFieldDefinition; -use Drupal\Core\Field\ChangedFieldItemList; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; @@ -176,6 +175,13 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C */ protected $enforceRevisionTranslationAffected = []; + /** + * Local cache for fields to skip from the checking for translation changes. + * + * @var array + */ + protected static $fieldsToSkipFromTranslationChangesCheck = []; + /** * {@inheritdoc} */ @@ -772,11 +778,11 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C * {@inheritdoc} */ public function onChange($name) { - // Check if the changed name is the value of an entity key and if the value - // of that is currently cached, if so, reset it. Exclude the bundle from - // that check, as it ready only and must not change, unsetting it could + // Check if the changed name is the value of any entity keys and if any of + // those values are currently cached, if so, reset it. Exclude the bundle + // from that check, as it ready only and must not change, unsetting it could // lead to recursions. - if ($key = array_search($name, $this->getEntityType()->getKeys())) { + foreach (array_keys($this->getEntityType()->getKeys(), $name, TRUE) as $key) { if ($key != 'bundle') { if (isset($this->entityKeys[$key])) { unset($this->entityKeys[$key]); @@ -799,7 +805,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C // Update the default internal language cache. $this->setDefaultLangcode(); if (isset($this->translations[$this->defaultLangcode])) { - $message = SafeMarkup::format('A translation already exists for the specified language (@langcode).', ['@langcode' => $this->defaultLangcode]); + $message = new FormattableMarkup('A translation already exists for the specified language (@langcode).', ['@langcode' => $this->defaultLangcode]); throw new \InvalidArgumentException($message); } $this->updateFieldLangcodes($this->defaultLangcode); @@ -810,7 +816,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C $items = $this->get($this->langcodeKey); if ($items->value != $this->activeLangcode) { $items->setValue($this->activeLangcode, FALSE); - $message = SafeMarkup::format('The translation language cannot be changed (@langcode).', ['@langcode' => $this->activeLangcode]); + $message = new FormattableMarkup('The translation language cannot be changed (@langcode).', ['@langcode' => $this->activeLangcode]); throw new \LogicException($message); } } @@ -821,7 +827,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C // read-only. See https://www.drupal.org/node/2443991. if (isset($this->values[$this->defaultLangcodeKey]) && $this->get($this->defaultLangcodeKey)->value != $this->isDefaultTranslation()) { $this->get($this->defaultLangcodeKey)->setValue($this->isDefaultTranslation(), FALSE); - $message = SafeMarkup::format('The default translation flag cannot be changed (@langcode).', ['@langcode' => $this->activeLangcode]); + $message = new FormattableMarkup('The default translation flag cannot be changed (@langcode).', ['@langcode' => $this->activeLangcode]); throw new \LogicException($message); } break; @@ -846,8 +852,8 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C // Populate entity translation object cache so it will be available for all // translation objects. - if ($langcode == $this->activeLangcode) { - $this->translations[$langcode]['entity'] = $this; + if (!isset($this->translations[$this->activeLangcode]['entity'])) { + $this->translations[$this->activeLangcode]['entity'] = $this; } // If we already have a translation object for the specified language we can @@ -1377,7 +1383,11 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C * An array of field names. */ protected function getFieldsToSkipFromTranslationChangesCheck() { - return $this->traitGetFieldsToSkipFromTranslationChangesCheck($this); + $bundle = $this->bundle(); + if (!isset(static::$fieldsToSkipFromTranslationChangesCheck[$this->entityTypeId][$bundle])) { + static::$fieldsToSkipFromTranslationChangesCheck[$this->entityTypeId][$bundle] = $this->traitGetFieldsToSkipFromTranslationChangesCheck($this); + } + return static::$fieldsToSkipFromTranslationChangesCheck[$this->entityTypeId][$bundle]; } /** @@ -1413,6 +1423,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C // possible or be meaningless. /** @var \Drupal\Core\Entity\ContentEntityBase $translation */ $translation = $original->getTranslation($this->activeLangcode); + $langcode = $this->language()->getId(); // The list of fields to skip from the comparision. $skip_fields = $this->getFieldsToSkipFromTranslationChangesCheck(); @@ -1428,18 +1439,10 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C if (in_array($field_name, $skip_fields, TRUE) || ($skip_untranslatable_fields && !$definition->isTranslatable())) { continue; } - $field = $this->get($field_name); - // When saving entities in the user interface, the changed timestamp is - // automatically incremented by ContentEntityForm::submitForm() even if - // nothing was actually changed. Thus, the changed time needs to be - // ignored when determining whether there are any actual changes in the - // entity. - if (!($field instanceof ChangedFieldItemList) && !$definition->isComputed()) { - $items = $field->filterEmptyItems(); - $original_items = $translation->get($field_name)->filterEmptyItems(); - if (!$items->equals($original_items)) { - return TRUE; - } + $items = $this->get($field_name)->filterEmptyItems(); + $original_items = $translation->get($field_name)->filterEmptyItems(); + if ($items->hasAffectingChanges($original_items, $langcode)) { + return TRUE; } }