EntityChangedTrait.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. /**
  4. * Provides a trait for accessing changed time.
  5. */
  6. trait EntityChangedTrait {
  7. /**
  8. * Returns the timestamp of the last entity change across all translations.
  9. *
  10. * @return int
  11. * The timestamp of the last entity save operation across all
  12. * translations.
  13. */
  14. public function getChangedTimeAcrossTranslations() {
  15. $changed = $this->getUntranslated()->getChangedTime();
  16. foreach ($this->getTranslationLanguages(FALSE) as $language) {
  17. $translation_changed = $this->getTranslation($language->getId())->getChangedTime();
  18. $changed = max($translation_changed, $changed);
  19. }
  20. return $changed;
  21. }
  22. /**
  23. * Gets the timestamp of the last entity change for the current translation.
  24. *
  25. * @return int
  26. * The timestamp of the last entity save operation.
  27. */
  28. public function getChangedTime() {
  29. return $this->get('changed')->value;
  30. }
  31. /**
  32. * Sets the timestamp of the last entity change for the current translation.
  33. *
  34. * @param int $timestamp
  35. * The timestamp of the last entity save operation.
  36. *
  37. * @return $this
  38. */
  39. public function setChangedTime($timestamp) {
  40. $this->set('changed', $timestamp);
  41. return $this;
  42. }
  43. }