home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (content.diff.inc)

This example Drupal source code file (content.diff.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, as, field, foreach, function, if, isset, items, new_node, php, queried_ids, result, return, titles

The content.diff.inc Drupal example source code

<?php
// $Id: content.diff.inc,v 1.2.2.7 2010/06/09 01:13:08 karens Exp $

/**
 * @file hook_diff() implementations for CCK (especially fields).
 *
 * These should use a field-hook so the data for the diff is
 * field-type specific.
 */

/**
 * Implementation of hook_diff()
 */
function content_diff($old_node, $new_node) {
  $result = array();
  // Prevent against invalid 'nodes' built by broken 3rd party code.
  if (isset($new_node->type)) {
    $type = content_types($new_node->type);
    $field_types = _content_field_types();
    foreach ($type['fields'] as $field) {
      // Ignore fields the current user is not allowed to view.
      if (!content_access('view', $field, NULL, $new_node)) {
        continue;
      }
      $function = $field_types[$field['type']]['module'] . '_content_diff_values';
      $function = function_exists($function) ? $function : 'content_content_diff_values';
      $old_values = array();
      $new_values = array();
      if (isset($old_node->$field['field_name'])) {
        $old_values = $function($old_node, $field, $old_node->$field['field_name']);
      }
      if (isset($new_node->$field['field_name'])) {
        $new_values = $function($new_node, $field, $new_node->$field['field_name']);
      }
      if ($old_values || $new_values) {
        $result[$field['field_name']] = array(
          '#name' => $field['widget']['label'],
          '#old' => $old_values,
          '#new' => $new_values,
          '#weight' => $field['widget']['weight'],
          '#format' => array(
            'show_header' => FALSE,
          ),
        );
      }
    }
  }
  return $result;
}

/**
 * Default 'implementation' of hook_content_diff_values.
 *
 * Note that diff.module takes care of running check_plain on the output.
 */
function content_content_diff_values($node, $field, $items) {
  $return = array();
  foreach ($items as $item) {
    foreach (explode("\n", $item['value']) as $i) {
      $return[] = $i;
    }
  }
  return $return;
}

if (module_exists('userreference')) {
  /**
   * Implementation of hook_content_diff_values.
   */
  function userreference_content_diff_values($node, $field, $items) {
    static $titles = array();
    // Gather ids.
    $ids = array();
    foreach ($items as $item) {
      if ($item['uid'] && is_numeric($item['uid'])) {
        $ids[] = $item['uid'];
      }
    }
    // Fetch titles we don't know yet.
    $queried_ids = array_diff($ids, array_keys($titles));
    if ($queried_ids) {
      $result = db_query('SELECT uid, name FROM {users} WHERE uid IN ('. db_placeholders($queried_ids) .')', $queried_ids);
      while ($row = db_fetch_array($result)) {
        $titles[$row['uid']] = $row['name'];
      }
    }
    // Return result.
    $return = array();
    foreach ($items as $item) {
      if ($item['uid'] && isset($titles[$item['uid']])) {
        $return[] = $titles[$item['uid']];
      }
    }
    return $return;
  }
}

if (module_exists('nodereference')) {
  /**
   * Implementation of hook_content_diff_values.
   */
  function nodereference_content_diff_values($node, $field, $items) {
    static $titles = array();
    // Gather ids.
    $ids = array();
    foreach ($items as $item) {
      if ($item['nid'] && is_numeric($item['nid'])) {
        $ids[] = $item['nid'];
      }
    }
    // Fetch titles we don't know yet.
    $queried_ids = array_diff($ids, array_keys($titles));
    if ($queried_ids) {
      $result = db_query('SELECT nid, title FROM {node} WHERE nid IN ('. db_placeholders($queried_ids) .')', $queried_ids);
      while ($row = db_fetch_array($result)) {
        $titles[$row['nid']] = $row['title'];
      }
    }
    // Return result.
    $return = array();
    foreach ($items as $item) {
      if ($item['nid'] && isset($titles[$item['nid']])) {
        $return[] = $titles[$item['nid']];
      }
    }
    return $return;
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal content.diff.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.