EntityChangesDetectionTrait.php 1.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php

namespace Drupal\Core\Entity;

/**
 * Provides helper methods to detect changes in an entity object.
 *
 * @internal This may be replaced by a proper entity comparison handler.
 */
trait EntityChangesDetectionTrait {

  /**
   * Returns an array of field names to skip when checking for changes.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   A content entity object.
   *
   * @return string[]
   *   An array of field names.
   */
  protected function getFieldsToSkipFromTranslationChangesCheck(ContentEntityInterface $entity) {
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
    $entity_type = $entity->getEntityType();

    // A list of known revision metadata fields which should be skipped from
    // the comparision.
    $fields = [
      $entity_type->getKey('revision'),
      $entity_type->getKey('revision_translation_affected'),
    ];
    $fields = array_merge($fields, array_values($entity_type->getRevisionMetadataKeys()));

33 34 35 36 37 38 39
    // Computed fields should be skipped by the check for translation changes.
    foreach (array_diff_key($entity->getFieldDefinitions(), array_flip($fields)) as $field_name => $field_definition) {
      if ($field_definition->isComputed()) {
        $fields[] = $field_name;
      }
    }

40 41 42 43
    return $fields;
  }

}