function hook_field_update

You are here

7 field.api.php hook_field_update($entity_type, $entity, $field, $instance, $langcode, &$items)

Define custom update behavior for this module's field data.

This hook is invoked from field_attach_update() on the module that defines a field, during the process of updating an entity object (node, taxonomy term, etc.). It is invoked just before the data for this field on the particular entity object is updated into field storage. Only field modules that are storing or tracking information outside the standard field storage mechanism need to implement this hook.

Parameters

$entity_type: The type of $entity.

$entity: The entity for the operation.

$field: The field structure for the operation.

$instance: The instance structure for $field on $entity's bundle.

$langcode: The language associated with $items.

$items: $entity->{$field['field_name']}[$langcode], or an empty array if unset.

See also

hook_field_insert()

hook_field_delete()

Related topics

3 functions implement hook_field_update()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

field_test_field_update in modules/field/tests/field_test.field.inc
Implements hook_field_update().
file_field_update in modules/file/file.field.inc
Implements hook_field_update().
image_field_update in modules/image/image.field.inc
Implements hook_field_update().
3 invocations of hook_field_update()
field_attach_update in modules/field/field.attach.inc
Save field data for an existing entity.
update_check_requirements in ./update.php
Checks update requirements and reports errors and (optionally) warnings.
user_save in modules/user/user.module
Save changes to a user account or add a new user.

File

modules/field/field.api.php, line 507

Code

function hook_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') {
    $first_call = &drupal_static(__FUNCTION__, array());

    // We don't maintain data for old revisions, so clear all previous values
    // from the table. Since this hook runs once per field, per object, make
    // sure we only wipe values once.
    if (!isset($first_call[$entity->nid])) {
      $first_call[$entity->nid] = FALSE;
      db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
    }
    // Only save data to the table if the node is published.
    if ($entity->status) {
      $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
      foreach ($items as $item) {
        $query->values(array(
          'nid' => $entity->nid,
          'tid' => $item['tid'],
          'sticky' => $entity->sticky,
          'created' => $entity->created,
        ));
      }
      $query->execute();
    }
  }
}

Comments

I recommend to use hook_field_storage_pre_update().