You are here

function gmap_taxonomy_node_update in GMap Module 7.2

Same name and namespace in other branches
  1. 7 gmap_taxonomy.module \gmap_taxonomy_node_update()

Implements hook_node_update().

@todo move this to GmapNodeCRUDi class

1 call to gmap_taxonomy_node_update()
gmap_taxonomy_node_insert in ./gmap_taxonomy.module
Implements hook_node_insert().

File

./gmap_taxonomy.module, line 176
GMap Taxonomy Markers

Code

function gmap_taxonomy_node_update($node) {

  // Remove the marker association if present. We'll read it later if it's
  // still applicable.
  db_delete('gmap_taxonomy_node')
    ->condition('nid', $node->nid)
    ->execute();

  // Get list of taxonomy_term_reference field names.
  $gmap_taxonomy_fields = gmap_taxonomy_get_instances();

  // Get the markers.
  $gmap_taxonomy_markers = gmap_taxonomy_get_markers();

  // If this node doesn't use taxonomy_term_reference fields at all,
  // skip the rest.
  if (!isset($gmap_taxonomy_fields[$node->type])) {
    return;
  }

  // Loop through the page's terms and insert markers if applicable.
  foreach ($gmap_taxonomy_fields[$node->type] as $fieldname) {

    // Get this node's field values for this field.
    $terms = field_get_items('node', $node, $fieldname, $node->language);

    // Skip if term has no value.
    if (!$terms) {
      continue;
    }

    // Otherwise loop through and set marker for term, if term has marker.
    foreach ($terms as $term) {
      if (!isset($gmap_taxonomy_markers[$term['tid']])) {
        continue;
      }

      // Insert to gmap_taxonomy table.
      db_insert('gmap_taxonomy_node')
        ->fields(array(
        'nid' => $node->nid,
        'tid' => $term['tid'],
        'marker' => $gmap_taxonomy_markers[$term['tid']],
      ))
        ->execute();

      // because only 1 term marker per node is allowed (db schema constraint) break here
      // @see https://www.drupal.org/node/2043673
      break;
    }
  }
}