function field_attach_update

Saves field data for an existing entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity with fields to save.

Related topics

7 calls to field_attach_update()
FieldAttachOtherTest::testFieldAttachCache in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Test field cache.
FieldAttachStorageTest::testFieldAttachDelete in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php
Test field_attach_delete().
FieldAttachStorageTest::testFieldAttachSaveLoad in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php
Check field values insert, update and load.
FieldAttachStorageTest::testFieldAttachSaveMissingData in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php
Tests insert and update with missing or NULL fields.
FieldSqlStorageTest::testFieldAttachInsertAndUpdate in drupal/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php
Reads mysql to verify correct data is written when using insert and update.

... See full list

File

drupal/core/modules/field/field.attach.inc, line 1230
Field attach API, allowing entities (nodes, users, ...) to be 'fieldable'.

Code

function field_attach_update(EntityInterface $entity) {

  // Ensure we are working with a BC mode entity.
  $entity = $entity
    ->getBCEntity();
  _field_invoke('update', $entity);

  // Let any module update field data before the storage engine, accumulating
  // saved fields along the way.
  $skip_fields = array();
  foreach (module_implements('field_storage_pre_update') as $module) {
    $function = $module . '_field_storage_pre_update';
    $function($entity, $skip_fields);
  }

  // Collect the storage backends used by the remaining fields in the entities.
  $storages = array();
  foreach (field_info_instances($entity
    ->entityType(), $entity
    ->bundle()) as $instance) {
    $field = field_info_field_by_id($instance['field_id']);
    $field_id = $field['uuid'];
    $field_name = $field['field_name'];

    // Leave the field untouched if $entity comes with no $field_name property,
    // but empty the field if it comes as a NULL value or an empty array.
    // Function property_exists() is slower, so we catch the more frequent
    // cases where it's an empty array with the faster isset().
    if (isset($entity->{$field_name}) || property_exists($entity, $field_name)) {

      // Collect the storage backend if the field has not been written yet.
      if (!isset($skip_fields[$field_id])) {
        $storages[$field['storage']['type']][$field_id] = $field_id;
      }
    }
  }

  // Field storage backends save any remaining unsaved fields.
  foreach ($storages as $storage => $fields) {
    $storage_info = field_info_storage_types($storage);
    module_invoke($storage_info['module'], 'field_storage_write', $entity, FIELD_STORAGE_UPDATE, $fields);
  }

  // Let other modules act on updating the entity.
  module_invoke_all('field_attach_update', $entity);
  $entity_info = $entity
    ->entityInfo();
  if ($entity_info['field_cache']) {
    cache('field')
      ->delete('field:' . $entity
      ->entityType() . ':' . $entity
      ->id());
  }
}