7.x field.api.php hook_field_storage_pre_update($entity_type, $entity, &$skip_fields)

Act before the storage backends update field data.

This hook allows modules to store data before the Field Storage API, optionally preventing the field storage module from doing so.

Parameters

$entity_type: The type of $entity; for example, 'node' or 'user'.

$entity: The entity with fields to save.

$skip_fields: An array keyed by field IDs whose data has already been written and therefore should not be written again. The values associated with these keys are not specified.

Return value

Saved field IDs are set set as keys in $skip_fields.

Related topics

1 function implements hook_field_storage_pre_update()

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

forum_field_storage_pre_update in modules/forum/forum.module
Implements hook_field_storage_pre_update().
1 invocation of hook_field_storage_pre_update()
field_attach_update in modules/field/field.attach.inc
Save field data for an existing entity.

File

modules/field/field.api.php, line 2268
Hooks provided by the Field module.

Code

function hook_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
  $first_call = &drupal_static(__FUNCTION__, array());

  if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
    // We don't maintain data for old revisions, so clear all previous values
    // from the table. Since this hook runs once per field, per entity, make
    // sure we only wipe values once.
    if (!isset($first_call[$entity->nid])) {
      $first_call[$entity->nid] = FALSE;
      db_delete('forum_index')->condition('nid', $entity->nid)->execute();
    }
    // Only save data to the table if the node is published.
    if ($entity->status) {
      $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
      foreach ($entity->taxonomy_forums as $language) {
        foreach ($language as $delta) {
          $query->values(array(
            'nid' => $entity->nid,
            'title' => $entity->title,
            'tid' => $delta['value'],
            'sticky' => $entity->sticky,
            'created' => $entity->created,
            'comment_count' => 0,
            'last_comment_timestamp' => $entity->created,
          ));
        }
      }
      $query->execute();
      // The logic for determining last_comment_count is fairly complex, so
      // call _forum_update_forum_index() too.
      _forum_update_forum_index($entity->nid);
    }
  }
}

Comments

fonant’s picture

$skip_fields is an array where the keys are integer field IDs, not field names.

Here's some code I've used for a project where we wanted to stop any field data being saved for anonymous users:

/**
 * Implements hook_field_storage_pre_update().
 *
 * Prevent field data being saved for UID=0.
 */
function MYMODULE_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
  if ($entity_type == 'user' and $entity->uid == 0) {
    $fields = field_info_instances('user');
    foreach ($fields['user'] as $field_name => $field) {
      $skip_fields[$field['field_id']] = $field_name;
    }
    watchdog('MYMODULE', 'Field update blocked for UID=0. Skip_fields: <pre>' . print_r($skip_fields, TRUE) . '</pre>');
  }
}