You are here

function nodereferrer_nodeapi in NodeReferrer 6

Same name and namespace in other branches
  1. 5 nodereferrer.module \nodereferrer_nodeapi()

Implementation of hook_nodeapi().

File

./nodereferrer.module, line 465
Defines a field type for backlinking referencing nodes.

Code

function nodereferrer_nodeapi($node, $op) {
  switch ($op) {
    case 'insert':
    case 'update':
    case 'delete':

      // Clear content cache to help maintain proper display of nodes.
      $nids = array();
      $type = content_types($node->type);
      if (!empty($type['fields'])) {
        foreach ($type['fields'] as $field) {

          // Add referenced nodes to nids. This will clean up nodereferrer fields
          // when the referencing node is updated.
          if ($field['type'] == 'nodereference') {
            $node_field = isset($node->{$field}['field_name']) ? $node->{$field}['field_name'] : array();
            foreach ($node_field as $delta => $item) {
              $nids[$item['nid']] = $item['nid'];
            }
          }
        }
      }

      // Clear Content cache for nodes that reference the node that is being updated.
      // This will keep nodereference fields up to date when referred nodes are
      // updated. @note this currenlty doesn't work all that well since nodereference
      // doesn't respect publishing states or access control.
      if (isset($node->nid)) {
        $referrers = nodereferrer_referrers($node->nid);
        $referrer_nids = array_keys($referrers);
        $nids = array_merge($nids, $referrer_nids);
      }
      foreach ($nids as $nid) {
        $cid = "content:{$nid}:";

        // define a table to delete from or else this complains
        cache_clear_all($cid, 'cache_content', TRUE);
      }
  }
}