79acf7c8dca472c89748aee4eb26efb83b128528
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d7 / hook / field_update.twig
1 /**
2  * Implements hook_field_update().
3  */
4 function {{ machine_name }}_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
5   if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') {
6     $first_call = &drupal_static(__FUNCTION__, array());
7
8     // We don't maintain data for old revisions, so clear all previous values
9     // from the table. Since this hook runs once per field, per object, make
10     // sure we only wipe values once.
11     if (!isset($first_call[$entity->nid])) {
12       $first_call[$entity->nid] = FALSE;
13       db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
14     }
15     // Only save data to the table if the node is published.
16     if ($entity->status) {
17       $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
18       foreach ($items as $item) {
19         $query->values(array(
20           'nid' => $entity->nid,
21           'tid' => $item['tid'],
22           'sticky' => $entity->sticky,
23           'created' => $entity->created,
24         ));
25       }
26       $query->execute();
27     }
28   }
29 }