On node insert I want to save another node with data taken from first node into other Drupal database. The node in second site has 3 fields: title, image field, is_public. Both Drupal installation shares same files directories.
Code:
$node = new stdClass;
$node->type = GALLERY_IMAGE_TYPE;
node_object_prepare($node);
$node->is_new = True;
$node->language = LANGUAGE_NONE;
$node->title = $file->filename;
$node->uid = 1;
$node->status = 1;
$node->{GALLERY_IMAGE_FIELD_NAME}[LANGUAGE_NONE][] = array(
'fid' => $file->fid,
'alt' => $image['alt'],
'title' => $image['title'],
);
$node->{GALLERY_IMAGE_PUBLIC_FIELD_NAME}[LANGUAGE_NONE][]['value'] = $is_public;
node_save($node);
db_set_active();
$node structure before node_save is similar to node structure created from admin panel and dump on hook_node_view:
Dump from hook_node_view in second database:
[field_file_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 9
[display] => 1
[description] =>
[upload_button] => Upload
[remove_button] => Remove
[upload] =>
[width] => 415
[height] => 332
[alt] =>
[title] =>
)
)
)
[field_file_image_is_public] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 1
)
)
)
Node structure before node_save:
stdClass Object
(
[type] => image
[status] => 1
[promote] => 1
[sticky] => 0
[uid] => 1
[created] => 1355514057
[revision] =>
[comment] => 2
[menu] => Array
(
[link_title] =>
[mlid] => 0
[plid] => 0
[menu_name] => main-menu
[weight] => 0
[options] => Array
(
)
[module] => menu
[expanded] => 0
[hidden] => 0
[has_children] => 0
[customized] => 0
[parent_depth_limit] => 8
)
[language] => und
[title] => wyoming-rocky-mountains.jpg
[field_file_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 13
[display] => 1
[description] =>
[upload_button] => Upload
[remove_button] => Remove
[upload] =>
[width] => 415
[height] => 332
[alt] =>
[title] =>
)
)
)
[field_file_image_is_public] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 1
)
)
)
)
$node structure after node_save:
stdClass Object
(
[vid] => 13
[uid] => 1
[title] => wyoming-rocky-mountains.jpg
[log] =>
[status] => 1
[comment] => 2
[promote] => 1
[sticky] => 0
[nid] => 13
[type] => image
[language] => und
[created] => 1355514057
[changed] => 1355514057
[tnid] => 0
[translate] => 0
[revision_timestamp] => 1355514057
[revision_uid] => 1
[rdf_mapping] => Array
(
[rdftype] => Array
(
[0] => sioc:Item
[1] => foaf:Document
)
[title] => Array
(
[predicates] => Array
(
[0] => dc:title
)
)
[created] => Array
(
[predicates] => Array
(
[0] => dc:date
[1] => dc:created
)
[datatype] => xsd:dateTime
[callback] => date_iso8601
)
[changed] => Array
(
[predicates] => Array
(
[0] => dc:modified
)
[datatype] => xsd:dateTime
[callback] => date_iso8601
)
[body] => Array
(
[predicates] => Array
(
[0] => content:encoded
)
)
[uid] => Array
(
[predicates] => Array
(
[0] => sioc:has_creator
)
[type] => rel
)
[name] => Array
(
[predicates] => Array
(
[0] => foaf:name
)
)
[comment_count] => Array
(
[predicates] => Array
(
[0] => sioc:num_replies
)
[datatype] => xsd:integer
)
[last_activity] => Array
(
[predicates] => Array
(
[0] => sioc:last_activity_date
)
[datatype] => xsd:dateTime
[callback] => date_iso8601
)
)
[cid] => 0
[last_comment_timestamp] => 1355514057
[last_comment_name] =>
[last_comment_uid] => 1
[comment_count] => 0
[name] => admin
[picture] => 0
[data] => b:0;
)
I don't understand why custom fields are wipeout.
EDIT
Anyway I switched from node type to entity. The issue still present. I put test code to Drupal installation which is using database called 'gallery':
$e = entity_create('model', array('type' => 'image'));
$ew = entity_metadata_wrapper('model',$e);
$ew->field_entity_image_is_public->set(1);
$ew->field_entity_image->set(array(
'fid' => 12,
'alt' => 'alt',
'title' => 'title',
));
$ew->field_entity_image_name->set('image name');
$ew->save();
It works ok, but when I try to use similar snipper in other installation:
$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
db_set_active('gallery');
file_save($file);
$e = entity_create('model', array('type' => 'image'));
$ew = entity_metadata_wrapper('model',$e);
$ew->field_entity_image_is_public->set(1);
$ew->field_entity_image->set(array(
'fid' => $file->fid,
'alt' => $image['alt'],
'title' => $image['title'],
));
$ew->field_entity_image_name->set($file->filename);
$ew->save();
db_set_active();
Custom fields are not stored. Only record in file_managed table is inserted correctly.
All Drupal installations are on the same server so I thought that I can just switch to other installation database, but now I start thinking about using somekind of web service but it is pointless in this case :/
node_save()
, and not after.