You are here

function taxonomy_entity_index_field_attach_update in Taxonomy Entity Index 7

Implements hook_field_attach_update().

2 calls to taxonomy_entity_index_field_attach_update()
taxonomy_entity_index_field_attach_insert in ./taxonomy_entity_index.module
Implements hook_field_attach_insert().
taxonomy_entity_index_reindex_entity_type in ./taxonomy_entity_index.module
Batch callback; re-index all the terms for a given entity type.

File

./taxonomy_entity_index.module, line 114

Code

function taxonomy_entity_index_field_attach_update($entity_type, $entity) {
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);

  // The revision ID may be NULL.
  if (!isset($revision_id)) {
    $revision_id = $entity_id;
  }
  $taxonomy_fields = taxonomy_entity_index_get_taxonomy_field_names($entity_type, $bundle);
  if (empty($taxonomy_fields)) {

    // Clear previous taxonomy index for the specific revision ID of the entity
    // since we want to keep track of previous revisions.
    db_delete('taxonomy_entity_index')
      ->condition('entity_type', $entity_type)
      ->condition('entity_id', $entity_id)
      ->condition('revision_id', $revision_id)
      ->execute();
    return;
  }
  $terms = array();
  foreach ($taxonomy_fields as $field_name) {
    if ($items = field_get_items($entity_type, $entity, $field_name)) {
      foreach ($items as $delta => $item) {
        $terms[] = array(
          'tid' => $item['tid'],
          'field_name' => $field_name,
          'delta' => (string) $delta,
        );
      }
    }
  }

  // Compare stored data with new data.
  $query = db_select('taxonomy_entity_index');
  $query
    ->fields('taxonomy_entity_index', array(
    'tid',
    'field_name',
    'delta',
  ));
  $stored = $query
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);
  $needs_update = FALSE;
  if (count($stored) !== count($terms)) {
    $needs_update = TRUE;
  }
  if (!$needs_update) {
    $needs_update = !empty(array_udiff($stored, $terms, '_taxonomy_entity_index_compare_term_struct'));
  }
  if (!$needs_update) {
    $needs_update = !empty(array_udiff($terms, $stored, '_taxonomy_entity_index_compare_term_struct'));
  }
  if ($needs_update) {

    // Remove old data.
    db_delete('taxonomy_entity_index')
      ->condition('entity_type', $entity_type)
      ->condition('entity_id', $entity_id)
      ->condition('revision_id', $revision_id)
      ->execute();
    if (!empty($terms)) {
      $query = db_insert('taxonomy_entity_index');
      $query
        ->fields(array(
        'entity_type',
        'bundle',
        'entity_id',
        'revision_id',
        'field_name',
        'delta',
        'tid',
      ));
      foreach ($terms as $term) {
        $query
          ->values(array(
          'entity_type' => $entity_type,
          'bundle' => $bundle,
          'entity_id' => $entity_id,
          'revision_id' => $revision_id,
          'field_name' => $term['field_name'],
          'delta' => $term['delta'],
          'tid' => $term['tid'],
        ));
      }
      $query
        ->execute();
    }
  }
}