You are here

function diff_entity_body_rows in Diff 7.3

Creates an array of rows which represent the difference between two entities.

Parameters

object $left_entity: Entity for comparison which will be displayed on the left side.

object $right_entity: Entity for comparison which will be displayed on the right side.

array $context: The context used to render the diff.

1 call to diff_entity_body_rows()
_diff_body_rows in ./diff.pages.inc
Creates an array of rows which represent the difference between nodes.

File

./diff.pages.inc, line 312
Menu callbacks for hook_menu().

Code

function diff_entity_body_rows($entity_type, $left_entity, $right_entity, $context = array()) {

  // This is an unique index only, so no need for drupal_static().
  static $table_row_counter = 0;
  if ($theme = variable_get('diff_theme', 'default')) {
    drupal_add_css(drupal_get_path('module', 'diff') . "/css/diff.{$theme}.css");
  }
  $rows = array();
  $any_visible_change = FALSE;
  $context += array(
    'entity_type' => $entity_type,
    'states' => array(
      'raw',
    ),
    'view_mode' => 'diff_standard',
  );
  $state = current($context['states']);
  $entity_diffs = diff_compare_entities($left_entity, $right_entity, $context);

  // Track line numbers between multiple diffs.
  $line_stats = array(
    'counter' => array(
      'x' => 0,
      'y' => 0,
    ),
    'offset' => array(
      'x' => 0,
      'y' => 0,
    ),
  );

  // Render diffs for each.
  foreach ($entity_diffs as $entity_diff) {
    $show_header = !empty($entity_diff['#name']);

    // These are field level settings.
    if ($show_header && isset($entity_diff['#settings']['show_header'])) {
      $show_header = $show_header && $entity_diff['#settings']['show_header'];
    }

    // Line counting and line header options.
    if (empty($entity_diff['#settings']['line_counter'])) {
      $line_counter = FALSE;
    }
    else {
      $line_counter = $entity_diff['#settings']['line_counter'];
    }

    // Every call to 'line' resets the counters.
    if ($line_counter) {
      $line_stats['counter']['x'] = 0;
      $line_stats['counter']['y'] = 0;
      if ($line_counter == 'line' && 0) {
        $line_stats['offset']['x'] = 0;
        $line_stats['offset']['y'] = 0;
      }
      $line_stats_ref = $line_stats;
    }
    else {
      $line_stats_ref = NULL;
    }
    list($left, $right) = diff_extract_state($entity_diff, $state);
    if ($entity_diff_rows = diff_get_rows($left, $right, $line_counter && $line_counter != 'hidden', $line_stats_ref)) {
      if ($line_counter && $line_counter != 'line') {
        $line_stats['offset']['x'] += $line_stats_ref['counter']['x'];
        $line_stats['offset']['y'] += $line_stats_ref['counter']['y'];
      }
      if ($show_header) {
        $rows['diff-header-' . $table_row_counter++] = array(
          array(
            'data' => t('Changes to %name', array(
              '%name' => $entity_diff['#name'],
            )),
            'class' => 'diff-section-title',
            'colspan' => 4,
          ),
        );
      }

      // To avoid passing counter to the Diff engine, index rows manually here
      // to allow modules to interact with the table. i.e. no array_merge().
      foreach ($entity_diff_rows as $row) {
        $rows['diff-row-' . $table_row_counter++] = $row;
      }
      $any_visible_change = TRUE;
    }
  }
  if (!$any_visible_change) {
    $rows['diff-empty-' . $table_row_counter++] = array(
      array(
        'data' => t('No visible changes'),
        'class' => 'diff-section-title',
        'colspan' => 4,
      ),
    );
  }
  return $rows;
}