Skip to main content
2 of 4
added 173 characters in body
Codium
  • 5k
  • 11
  • 41
  • 91

Why node custom fields weren't saved when adding node programmatically?

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:

$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
file_save($file);
$image['fid'] = $file->fid;
$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.

Codium
  • 5k
  • 11
  • 41
  • 91