EntityChangesDetectionTrait.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. /**
  4. * Provides helper methods to detect changes in an entity object.
  5. *
  6. * @internal This may be replaced by a proper entity comparison handler.
  7. */
  8. trait EntityChangesDetectionTrait {
  9. /**
  10. * Returns an array of field names to skip when checking for changes.
  11. *
  12. * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  13. * A content entity object.
  14. *
  15. * @return string[]
  16. * An array of field names.
  17. */
  18. protected function getFieldsToSkipFromTranslationChangesCheck(ContentEntityInterface $entity) {
  19. /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
  20. $entity_type = $entity->getEntityType();
  21. // A list of known revision metadata fields which should be skipped from
  22. // the comparision.
  23. $fields = [
  24. $entity_type->getKey('revision'),
  25. $entity_type->getKey('revision_translation_affected'),
  26. ];
  27. $fields = array_merge($fields, array_values($entity_type->getRevisionMetadataKeys()));
  28. // Computed fields should be skipped by the check for translation changes.
  29. foreach (array_diff_key($entity->getFieldDefinitions(), array_flip($fields)) as $field_name => $field_definition) {
  30. if ($field_definition->isComputed()) {
  31. $fields[] = $field_name;
  32. }
  33. }
  34. return $fields;
  35. }
  36. }