3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
6 * Defines the 'changed' entity field type.
8 * Based on a field of this type, entity types can easily implement the
9 * EntityChangedInterface.
13 * label = @Translation("Last changed"),
14 * description = @Translation("An entity field containing a UNIX timestamp of when the entity has been last updated."),
16 * default_widget = "datetime_timestamp",
17 * default_formatter = "timestamp",
18 * list_class = "\Drupal\Core\Field\ChangedFieldItemList"
21 * @see \Drupal\Core\Entity\EntityChangedInterface
23 class ChangedItem extends CreatedItem {
28 public function preSave() {
31 // Set the timestamp to request time if it is not set.
33 $this->value = REQUEST_TIME;
36 // On an existing entity translation, the changed timestamp will only be
37 // set to the request time automatically if at least one other field value
38 // of the entity has changed. This detection does not run on new entities
39 // and will be turned off if the changed timestamp is set manually before
40 // save, for example during migrations or by using
41 // \Drupal\content_translation\ContentTranslationMetadataWrapperInterface::setChangedTime().
42 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
43 $entity = $this->getEntity();
44 /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
45 $original = $entity->original;
46 $langcode = $entity->language()->getId();
47 if (!$entity->isNew() && $original->hasTranslation($langcode)) {
48 $original_value = $original->getTranslation($langcode)->get($this->getFieldDefinition()->getName())->value;
49 if ($this->value == $original_value && $entity->hasTranslationChanges()) {
50 $this->value = REQUEST_TIME;