ChangedTestItem.php

Same filename in other branches
  1. 9 core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php
  2. 8.9.x core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php
  3. 11.x core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php

Namespace

Drupal\entity_test\Plugin\Field\FieldType

File

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

View source
<?php

namespace Drupal\entity_test\Plugin\Field\FieldType;

use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\ChangedFieldItemList;
use Drupal\Core\Field\Plugin\Field\FieldType\ChangedItem;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines the 'changed_test' entity field type.
 *
 * Wraps Drupal\Core\Field\Plugin\Field\FieldType\ChangedItem.
 *
 * @see \Drupal\Core\Entity\EntityChangedInterface
 */
class ChangedTestItem extends ChangedItem {
    
    /**
     * {@inheritdoc}
     */
    public function preSave() {
        parent::preSave();
        if ($this->value == \Drupal::time()->getRequestTime()) {
            // 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();
        }
    }

}

Classes

Title Deprecated Summary
ChangedTestItem Defines the 'changed_test' entity field type.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.