You are here

function node_reference_import_sort in Default Content 7

Same name and namespace in other branches
  1. 7.2 plugins/node_reference.inc \node_reference_import_sort()

Handles a sort to insure that ref nodes are import first

File

plugins/node_reference.inc, line 70

Code

function node_reference_import_sort($a, $b) {
  $fields = get_ref_fields();
  $a_children = array();
  $b_children = array();
  foreach ($fields as $field_name => $field) {
    if (isset($a->{$field_name})) {
      foreach ($a->{$field_name} as $lang => $items) {
        foreach ($items as $key => $item) {
          $a_children[] = $item['machine_name'];
        }
      }
    }
    if (isset($b->{$field_name})) {
      foreach ($b->{$field_name} as $lang => $items) {
        foreach ($items as $key => $item) {
          $b_children[] = $item['machine_name'];
        }
      }
    }
  }
  if (in_array($a->machine_name, $b_children) || empty($a_children)) {
    return -1;
  }
  elseif (in_array($b->machine_name, $a_children) || empty($b_children)) {
    return 1;
  }
  else {
    return 0;
  }
}