node_export_relation.node_reference.inc in Node export 6.3
Same filename and directory in other branches
The Node export relation node reference include.
Helps maintain node reference relationships between nodes during node export operations.
File
modules/node_export_relation/node_export_relation.node_reference.incView source
<?php
/**
* @file
* The Node export relation node reference include.
*
* Helps maintain node reference relationships between nodes during node export operations.
*/
/**
* Callback for node reference settings form.
*/
function node_export_relation_settings_form_node_reference(&$form, &$form_state) {
$form['node_reference'] = array(
'#type' => 'fieldset',
'#title' => t('Node references'),
);
/*
// Not supported yet.
$form['node_reference']['node_export_node_reference_auto_inc'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically include referenced nodes in exports'),
'#default' => variable_get('node_export_node_reference_auto_inc', TRUE),
);
*/
$form['node_reference']['node_export_node_reference_skip'] = array(
'#type' => 'checkbox',
'#title' => t('Skip referenced nodes that cannot be exported'),
'#default' => variable_get('node_export_node_reference_skip', TRUE),
'#description' => t('If this is disabled, node exports will fail if a referenced node cannot be exported, for example if the user performing the export does not have access.'),
);
}
/**
* Update node references after import.
*/
function node_export_relation_node_reference_update($nodes) {
foreach ($nodes as $node) {
$uuid[$node->uuid] = $node->nid;
}
foreach ($nodes as $node) {
$node_reference_fields = node_export_relation_node_reference_fields($node->type);
if (!empty($node_reference_fields)) {
foreach ($node_reference_fields as $node_reference_field) {
if (!empty($node->{$node_reference_field})) {
foreach ($node->{$node_reference_field} as $key => $node_reference) {
if (!empty($node_reference['nid'])) {
$node_uuid = node_export_relation_node_reference_map($node_reference['nid']);
$node->{$node_reference_field}[$key] = array(
'nid' => $uuid[$node_uuid],
);
}
}
}
}
}
node_save($node);
}
}
/**
* Recursively load node references.
*/
function node_export_relation_node_reference_load(&$nodes, $nid) {
// Alias for easy referencing.
$node =& $nodes[$nid];
// Handle uuid for the node.
$uuid = uuid_get_uuid('node', 'nid', $node->nid);
// Create UUID if not already created.
if (empty($uuid)) {
$uuid = uuid_set_uuid('node', 'nid', $node->nid);
}
// Get list of node reference fields for this node
$node_reference_fields = node_export_relation_node_reference_fields($node->type);
if (!empty($node_reference_fields)) {
foreach ($node_reference_fields as $node_reference_field) {
if (!empty($node->{$node_reference_field})) {
foreach ($node->{$node_reference_field} as $key => $node_reference) {
if (!empty($node_reference['nid'])) {
// Load the referenced nodes only if its not already loaded
// This will save if from infinite loop of back references
if (!isset($nodes[$node_reference['nid']])) {
$new_node = node_load($node_reference['nid']);
if (node_export_access_export($new_node)) {
$new_node = node_export_prepare_node($new_node);
$nodes[$new_node->nid] = $new_node;
// Recursively load references of new nodes
node_export_relation_node_reference_load($nodes, $new_node->nid);
}
elseif (!variable_get('node_export_node_reference_skip', TRUE)) {
// Set this node to FALSE to trigger an error in node export.
// Do not use $new_node in this code in case there is a problem with it.
$nodes[$node_reference['nid']] = FALSE;
// Add a warning to watchdog.
watchdog('node_export_relation', 'No access to export node reference %nid', array(
'%nid' => $node_reference['nid'],
), WATCHDOG_WARNING);
}
}
}
}
}
}
}
}
/**
* Statically cache old node ids for mapping.
*/
function node_export_relation_node_reference_map($id, $uuid = NULL, $type = 'node') {
static $map;
if (isset($uuid)) {
$map[$type][$id] = $uuid;
}
else {
return $map[$type][$id];
}
}
/**
* Get an array listing the names of all node reference fields.
*
* @return
* Array of all created node reference fields.
*/
function node_export_relation_node_reference_fields($content_type_name) {
// cache result
static $node_reference_fields = array();
if (empty($node_reference_fields[$content_type_name])) {
$ct = content_types($content_type_name);
$fields = $ct['fields'];
if (!empty($fields)) {
foreach ($fields as $name => $field) {
if ($field['type'] == 'nodereference') {
$node_reference_fields[$content_type_name][$name] = $name;
}
}
}
}
if (array_key_exists($content_type_name, $node_reference_fields)) {
return $node_reference_fields[$content_type_name];
}
else {
return NULL;
}
}
Functions
Name | Description |
---|---|
node_export_relation_node_reference_fields | Get an array listing the names of all node reference fields. |
node_export_relation_node_reference_load | Recursively load node references. |
node_export_relation_node_reference_map | Statically cache old node ids for mapping. |
node_export_relation_node_reference_update | Update node references after import. |
node_export_relation_settings_form_node_reference | Callback for node reference settings form. |