You are here

function nodereference_count_nodeapi in Nodereference Count 6

Implementation of hook_nodeapi().

Update the nodereference count of any referenced nodes on insert, update, or delete.

File

./nodereference_count.module, line 216
Defines a field type for counting the references to a node.

Code

function nodereference_count_nodeapi(&$node, $op) {
  switch ($op) {
    case 'presave':

      // Load the original node so we can update the count on previously referenced nodes.
      $node->original = node_load($node->nid);
      break;
    case 'insert':
    case 'update':
    case 'delete':

      // Get a list of fields for this content type and filter down to only the nodereference fields.
      $content_type = content_types($node->type);
      $nodereference_fields = array();
      foreach ($content_type['fields'] as $field) {
        if ($field['type'] == 'nodereference') {
          $nodereference_fields[$field['field_name']] = $field;
        }
      }

      // If there are no nodereferences for this content type then there is nothing to count.
      if (empty($nodereference_fields)) {
        return;
      }

      // Get a list of any nodereference count fields that are counting the nodereference fields.
      $all_fields = content_fields();
      $nodereference_count_fields = array();
      foreach ($all_fields as $count_field) {
        if ($count_field['type'] == 'nodereference_count') {
          foreach ($nodereference_fields as $nodereference_field) {
            $counted_field = $count_field['referenceable_fields'][$nodereference_field['field_name']];
            if (isset($counted_field) && $counted_field === $nodereference_field['field_name']) {
              $nodereference_count_fields[] = array(
                'nodereference' => $nodereference_field['field_name'],
                'count' => $count_field,
              );
            }
          }
        }
      }

      // If there are no nodereferences being counted from this content type then there is nothing to count.
      if (empty($nodereference_count_fields)) {
        return;
      }

      // Loop through the array of counted fields and update the counts.
      foreach ($nodereference_count_fields as $nodereference_count_field) {
        $nodereferences = $node->{$nodereference_count_field}['nodereference'];
        $original_nodereferences = $node->original->{$nodereference_count_field}['nodereference'];
        foreach ($nodereferences as $index => $nodereference) {

          // If the reference has changed, update the count on the previous node.
          if (!empty($original_nodereferences[$index]['nid']) && $nodereference['nid'] != $original_nodereferences[$index]['nid']) {
            nodereference_count_update_count($nodereference_count_field['count'], $original_nodereferences[$index]['nid']);
          }

          // If there is no value in the nodereference field there is nothing to count.
          if (!empty($nodereference['nid'])) {
            nodereference_count_update_count($nodereference_count_field['count'], $nodereference['nid']);
          }
        }
      }
      break;
  }
}