You are here

function uuid_node_features_rebuild_nodes in UUID Features Integration 7

Runs the node import multiple times to resolve dependencies.

We might need several runs of ths function to resolve the dependencies created by reference fields. Those can only be resolved if the target node already exists.

Parameters

array $nodes: The nodes to process.

string $module: The module to rebuild for.

int $max_nesting: Maximal nesting level.

int $nesting_level: Current nesting level.

Return value

bool TRUE if all nodes could be restored.

1 call to uuid_node_features_rebuild_nodes()
uuid_node_features_rebuild in includes/uuid_node.features.inc
Implements hook_features_rebuild().

File

includes/uuid_node.features.inc, line 231
Features hooks for the uuid_node features component.

Code

function uuid_node_features_rebuild_nodes($nodes, $module, $max_nesting = 5, $nesting_level = 0) {

  // Max nesting level hit.
  if ($max_nesting < $nesting_level) {
    watchdog('UUID Features', 'Unable to restore nodes. Max nesting level reached.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  $second_run_nodes = array();
  foreach ($nodes as $data) {
    try {
      $node = (object) $data;
      $node->uid = 0;
      node_object_prepare($node);

      // Find the matching UUID, with a fresh cache.
      $nids = entity_get_id_by_uuid('node', array(
        $node->uuid,
      ));
      if (isset($nids[$node->uuid])) {
        $nid = array_key_exists($node->uuid, $nids) ? $nids[$node->uuid] : FALSE;
        $existing = node_load($nid, NULL, TRUE);
        if (!empty($existing)) {
          $node->nid = $existing->nid;
          $node->vid = $existing->vid;
        }
      }
      $entity_type = 'node';
      drupal_alter('uuid_entity_features_rebuild', $entity_type, $node, $data, $module);
      drupal_alter('uuid_node_features_rebuild', $node, $module);
      $node = node_submit($node);
      uuid_features_file_field_import($node, 'node', $module);
      entity_uuid_save('node', $node);
      if (!empty($node->path)) {
        $saved_node = entity_uuid_load('node', array(
          $node->uuid,
        ));
        $saved_node = reset($saved_node);
        if (empty($saved_node->path['alias'])) {
          $saved_node->path['alias'] = $node->path['alias'];
          node_save($saved_node);
        }
      }
    } catch (Exception $e) {
      $second_run_nodes[] = $data;
    }
  }
  if (!empty($second_run_nodes)) {
    return uuid_node_features_rebuild_nodes($second_run_nodes, $module, $max_nesting, ++$nesting_level);
  }
  lock_release('menu_rebuild');
  variable_set('menu_rebuild_needed', TRUE);
  return TRUE;
}