Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / ChangedItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 /**
6  * Defines the 'changed' entity field type.
7  *
8  * Based on a field of this type, entity types can easily implement the
9  * EntityChangedInterface.
10  *
11  * @FieldType(
12  *   id = "changed",
13  *   label = @Translation("Last changed"),
14  *   description = @Translation("An entity field containing a UNIX timestamp of when the entity has been last updated."),
15  *   no_ui = TRUE,
16  *   default_widget = "datetime_timestamp",
17  *   default_formatter = "timestamp",
18  *   list_class = "\Drupal\Core\Field\ChangedFieldItemList"
19  * )
20  *
21  * @see \Drupal\Core\Entity\EntityChangedInterface
22  */
23 class ChangedItem extends CreatedItem {
24
25   /**
26    * {@inheritdoc}
27    */
28   public function preSave() {
29     parent::preSave();
30
31     // Set the timestamp to request time if it is not set.
32     if (!$this->value) {
33       $this->value = REQUEST_TIME;
34     }
35     else {
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;
51         }
52       }
53     }
54   }
55
56 }