You are here

public function ChangedTestItem::preSave in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php \Drupal\entity_test\Plugin\Field\FieldType\ChangedTestItem::preSave()
  2. 9 core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php \Drupal\entity_test\Plugin\Field\FieldType\ChangedTestItem::preSave()

Defines custom presave behavior for field values.

This method is called during the process of saving an entity, just before values are written into storage. When storing a new entity, its identifier will not be available yet. This should be used to massage item property values or perform any other operation that needs to happen before values are stored. For instance this is the proper phase to auto-create a new entity for an entity reference field item, because this way it will be possible to store the referenced entity identifier.

Overrides ChangedItem::preSave

File

core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php, line 27

Class

ChangedTestItem
Defines the 'changed_test' entity field type.

Namespace

Drupal\entity_test\Plugin\Field\FieldType

Code

public function preSave() {
  parent::preSave();
  if ($this->value == REQUEST_TIME) {

    // During a test the request time is immutable. To allow tests of the
    // algorithm of
    // Drupal\Core\Field\Plugin\Field\FieldType\ChangedItem::preSave() we need
    // to set a real time value here. For the stability of the test, set the
    // time of the original language to the current time plus just over one
    // second to simulate two different request times.
    // @todo mock the time service in https://www.drupal.org/node/2908210.
    if ($this
      ->getEntity()
      ->language()
      ->isDefault()) {

      // Wait 1.1 seconds because time_sleep_until() is not reliable.
      time_sleep_until(time() + 1.1);
    }
    $this->value = time();
  }
}