You are here

public function FieldTestItem::postSave in Drupal 10

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

Defines custom post-save behavior for field values.

This method is called during the process of saving an entity, just after values are written into storage. This is useful mostly when the business logic to be implemented always requires the entity identifier, even when storing a new entity. For instance, when implementing circular entity references, the referenced entity will be created on pre-save with a dummy value for the referring entity identifier, which will be updated with the actual one on post-save.

In the rare cases where item properties depend on the entity identifier, massaging logic will have to be implemented on post-save and returning TRUE will allow them to be rewritten to the storage with the updated values.

Parameters

bool $update: Specifies whether the entity is being updated or created.

Return value

bool Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.

Overrides FieldItemBase::postSave

File

core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php, line 85

Class

FieldTestItem
Defines the 'field_test' entity field type.

Namespace

Drupal\entity_test\Plugin\Field\FieldType

Code

public function postSave($update) {

  // Determine whether the field value should be rewritten to the storage. We
  // always rewrite on create as we need to store a value including the entity
  // id.
  $resave = !$update || $this
    ->mustResave();
  if ($resave) {
    $entity = $this
      ->getEntity();
    $definition = $this
      ->getFieldDefinition();
    $name = $definition
      ->getName();
    $value = 'field_test:' . $name . ':' . $entity
      ->id() . ':' . static::$counter[$name];
    $this
      ->setValue($value);
  }
  return $resave;
}