ContentEntityStorageBase::hasFieldValueChanged

protected ContentEntityStorageBase::hasFieldValueChanged(FieldDefinitionInterface $field_definition, ContentEntityInterface $entity, ContentEntityInterface $original)

Checks whether the field values changed compared to the original entity.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: Field definition of field to compare for changes.

\Drupal\Core\Entity\ContentEntityInterface $entity: Entity to check for field changes.

\Drupal\Core\Entity\ContentEntityInterface $original: Original entity to compare against.

Return value

bool True if the field value changed from the original entity.

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 509

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

protected function hasFieldValueChanged(FieldDefinitionInterface $field_definition, ContentEntityInterface $entity, ContentEntityInterface $original) {
  $field_name = $field_definition->getName();
  $langcodes = array_keys($entity->getTranslationLanguages());
  if ($langcodes !== array_keys($original->getTranslationLanguages())) {
    // If the list of langcodes has changed, we need to save.
    return TRUE;
  }
  foreach ($langcodes as $langcode) {
    $items = $entity->getTranslation($langcode)->get($field_name)->filterEmptyItems();
    $original_items = $original->getTranslation($langcode)->get($field_name)->filterEmptyItems();
    // If the field items are not equal, we need to save.
    if (!$items->equals($original_items)) {
      return TRUE;
    }
  }

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

Please login to continue.