field_collection.diff.inc 3.61 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Provide diff field functions for the Field Collection.
6 7 8
 */

/**
9
 * Diff field callback for parsing Field Collection fields comparative values.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 */
function field_collection_field_diff_view($items, $context) {
  $diff_items = array();
  foreach ($items as $delta => $item) {
    $diff_items[$delta] = field_collection_field_render_revision($item, $context);
  }
  return $diff_items;
}

/**
 * Renders a field collection fields revision.
 */
function field_collection_field_render_revision($item, $context) {
  $entity_type = 'field_collection_item';
  $bundle_name = $context['field']['field_name'];
  $view_mode = $context['view_mode'];
  $entity = entity_revision_load($entity_type, $item['revision_id']);

Ra Mänd's avatar
Ra Mänd committed
28
  if (empty($entity)) {
29 30 31
    return array();
  }

32 33 34 35 36 37 38 39 40 41 42
  $field_context = $context;
  $field_context['entity_type'] = $entity_type;
  $field_context['bundle'] = $bundle_name;
  $field_context['entity'] = $entity;

  // Some fields piggy back the display settings, so we need to fake these by
  // ensuring that the field mode is always set.
  if (empty($view_mode)) {
    $field_context['custom_settings'] = FALSE;
  }
  $view_mode_settings = field_view_mode_settings($entity_type, $bundle_name);
Ra Mänd's avatar
Ra Mänd committed
43
  $actual_mode = !empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default';
44 45 46 47 48 49 50 51 52
  if (!isset($field_context['custom_settings'])) {
    $field_context['custom_settings'] = $actual_mode && $actual_mode == $view_mode;
  }

  $instances = field_info_instances($entity_type, $field_context['bundle']);
  usort($instances, '_field_collection_sort_items');

  $result = array();
  foreach ($instances as $instance) {
Ra Mänd's avatar
Ra Mänd committed
53 54
    // Any view mode is supported in relation to hiding fields, but only if
    // selected.
55 56 57 58 59 60 61 62 63
    if ($actual_mode && $instance['display'][$actual_mode]['type'] == 'hidden') {
      continue;
    }

    $field_name = $instance['field_name'];
    $field = field_info_field($field_name);

    // We provide a loose check on the field access.
    if (field_access('view', $field, $entity_type) || field_access('edit', $field, $entity_type)) {
64
      $langcode = !empty($entity) ? field_language($entity_type, $entity, $field_name) : LANGUAGE_NONE;
65 66 67 68 69 70
      $field_context['language'] = $langcode;
      $field_context['field'] = $field;
      $field_context['instance'] = $instance;
      $field_context['settings'] = diff_get_field_settings($field_context);
      $field_context['display'] = $instance['display'][$actual_mode];

71 72 73 74 75 76 77 78 79 80 81
      $old_items = array();
      $new_items = array();
      if (!empty($entity->{$field_name}[$langcode])) {
        $new_items = $entity->{$field_name}[$langcode];
      }

      $func = $field['module'] . '_field_diff_view_prepare';
      if (function_exists($func)) {
        $func($old_items, $new_items, $field_context);
      }

82 83 84 85 86
      $func = $field['module'] . '_field_diff_view';
      if (!function_exists($func)) {
        $func = 'diff_field_diff_view';
      }

87 88
      if (!empty($new_items)) {
        $raw_values = $func($new_items, $field_context);
89
        drupal_alter('field_diff_view', $raw_values, $new_items, $field_context);
90 91
        $values = array();
        foreach ($raw_values as $raw_value) {
Ra Mänd's avatar
Ra Mänd committed
92
          $values[] = is_array($raw_value) ? implode(', ', $raw_value) : $raw_value;
93 94
        }

Ra Mänd's avatar
Ra Mänd committed
95
        $result[] = check_plain($instance['label'] . ': ' . implode(', ', $values));
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      }
    }
  }

  return $result;
}

/**
 * Sort function for items order.
 */
function _field_collection_sort_items($a, $b) {
  $a_weight = (!empty($a['widget']['weight']) ? $a['widget']['weight'] : 0);
  $b_weight = (!empty($b['widget']['weight']) ? $b['widget']['weight'] : 0);
  return $a_weight - $b_weight;
}