You are here

function nodereferrer_nodeapi in NodeReferrer 5

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

Implementation of hook_nodeapi

File

./nodereferrer.module, line 333
Defines a field type for backlinking referencing nodes. @todo -clear content cache with nodeapi. -query nids for access on load/view..

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);
      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.
      $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);
      }
  }
}