You are here

function comment_alter_get_changed_fields in Comment Alter 7

Same name and namespace in other branches
  1. 8 comment_alter.module \comment_alter_get_changed_fields()

Returns the differences committed with a particular comment.

Uses the 'Diff' module to actually generate the differences.

Parameters

object $comment: The comment object.

string $langcode: The language code used for rendering the fields.

Return value

array An associative array with keys being the changed field names and values being lists of associative arrays with 3 keys:

  • name: field's name being changed (only for the first field value).
  • old: array of old field values.
  • new: array of new field values.

See also

diff_compare_entities()

1 call to comment_alter_get_changed_fields()
comment_alter_comment_view in ./comment_alter.module
Implements hook_comment_view().

File

./comment_alter.module, line 443
Provides UI to alter nodes' parameters from comment forms.

Code

function comment_alter_get_changed_fields($comment, $langcode) {
  $changed_fields = array();
  if (isset($comment->comment_alter) && isset($comment->comment_alter['new_vid'])) {
    $node = node_load($comment->nid);
    module_load_include('inc', 'diff', 'diff.pages');
    $old_node = node_load($comment->nid, $comment->comment_alter['old_vid']);
    $new_node = node_load($comment->nid, $comment->comment_alter['new_vid']);
    $context = array(
      'entity_type' => 'node',
      'states' => array(
        'raw',
      ),
      'view_mode' => 'diff_standard',
      'language' => $langcode,
    );
    $comment_alter_fields = comment_alter_get_alterable_fields($node->type);

    // Because the 'title_field' is 'hidden' on all display modes, the Diff
    // module will skip over it. However, the diff module has special support
    // for the 'title' property, so we swap 'title_field' for 'title'.
    if (($title_field_index = array_search('title_field', $comment_alter_fields)) !== FALSE) {
      $comment_alter_fields[$title_field_index] = 'title';
    }
    $diffs = diff_compare_entities($old_node, $new_node, $context);
    foreach ($diffs as $field_name => $diff) {

      // Only compare fields that belong to us.
      $field = field_info_field($field_name);
      if (field_access('view', $field, 'node') && in_array($field_name, $comment_alter_fields)) {
        $instance = field_info_instance('node', $field_name, $node->type);
        list($old, $new) = diff_extract_state($diff, $context['states'][0]);
        if ($old != $new) {
          if (!empty($instance['comment_alter_hide'])) {
            if (diff_node_revision_access($node)) {
              $changed_fields[$field_name][] = array(
                'name' => $diff['#name'],
                'changes' => l(t('View changes'), 'node/' . $node->nid . '/revisions/view/' . $old_node->vid . '/' . $new_node->vid),
              );
            }
            else {
              $changed_fields[$field_name][] = array(
                'name' => $diff['#name'],
                'old' => array(
                  '…',
                ),
                'new' => array(
                  '…',
                ),
              );
            }
            continue;
          }
          $rows = diff_get_rows($old, $new);
          $line = 0;
          foreach ($rows as $row) {
            $changed_fields[$field_name][] = array(
              'name' => $line ? '' : $diff['#name'],
              'old' => array(
                empty($row[1]['data']) ? '' : $row[1]['data'],
              ),
              'new' => array(
                empty($row[3]['data']) ? '' : $row[3]['data'],
              ),
            );
            $line++;
          }
        }
      }
    }
  }
  return $changed_fields;
}