ContentEntityBase::hasTranslationChanges

public ContentEntityBase::hasTranslationChanges()

Determines if the current translation of the entity has unsaved changes.

If the entity is translatable only translatable fields will be checked for changes.

Return value

bool TRUE if the current translation of the entity has changes.

Overrides ContentEntityInterface::hasTranslationChanges

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1187

Class

ContentEntityBase
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

public function hasTranslationChanges() {
  if ($this->isNew()) {
    return TRUE;
  }

  // $this->original only exists during save. See
  // \Drupal\Core\Entity\EntityStorageBase::save(). If it exists we re-use it
  // here for performance reasons.
  /** @var \Drupal\Core\Entity\ContentEntityBase $original */
  $original = $this->original ? $this->original : NULL;

  if (!$original) {
    $id = $this->getOriginalId() !== NULL ? $this->getOriginalId() : $this->id();
    $original = $this->entityManager()->getStorage($this->getEntityTypeId())->loadUnchanged($id);
  }

  // If the current translation has just been added, we have a change.
  $translated = count($this->translations) > 1;
  if ($translated && !$original->hasTranslation($this->activeLangcode)) {
    return TRUE;
  }

  // Compare field item current values with the original ones to determine
  // whether we have changes. If a field is not translatable and the entity is
  // translated we skip it because, depending on the use case, it would make
  // sense to mark all translations as changed or none of them. We skip also
  // computed fields as comparing them with their original values might not be
  // possible or be meaningless.
  /** @var \Drupal\Core\Entity\ContentEntityBase $translation */
  $translation = $original->getTranslation($this->activeLangcode);
  foreach ($this->getFieldDefinitions() as $field_name => $definition) {
    // @todo Avoid special-casing the following fields. See
    //    https://www.drupal.org/node/2329253.
    if ($field_name == 'revision_translation_affected' || $field_name == 'revision_id') {
      continue;
    }
    if (!$definition->isComputed() && (!$translated || $definition->isTranslatable())) {
      $items = $this->get($field_name)->filterEmptyItems();
      $original_items = $translation->get($field_name)->filterEmptyItems();
      if (!$items->equals($original_items)) {
        return TRUE;
      }
    }
  }

  return FALSE;
}
doc_Drupal
2016-10-29 08:56:57
Comments
Leave a Comment

Please login to continue.