scripts for direct DB creation of nodes with imagefield?

orican - April 14, 2007 - 09:28
Project:ImageField
Version:5.x-1.x-dev
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:duplicate
Description

Does anyone have an example that can create everything that is need to make a new node work properly with imagefield when using Drupal 5?

This works with the Image Module but not imagefield.

<?php
global $user;

$node = new StdClass();

$node->type = 'image';
$node->uid = 20;
     
$node->status = 1;
$node->promote = 1;
      
$node->title = 'This is the title for the content';
$node->body = 'The body of the node';
$node->teaser = 'The teaser of the node';
$node->comment = 2;

$node->images = array();

$node->file = $node->images['_original'] = "files/images/temp/lastmail.jpg";
_image_build_derivatives($node, TRUE);
$node->new_file = TRUE;

node_save($node);
?>

I attempted to make it the CCK way but no luck so far.

<?php
            $node
->field_img = array
            (
           
0 => array
                (
                   
'fid' =>  "upload",
                   
'title' => $file,   
                   
'alt' => $file,   
                   
'nid' => $node->nid,   
                   
'filename' => $file,   
                   
'filepath' => $dir.'/'.$file,
                   
'filemime' => 'image/jpeg',
                   
'filesize' => filesize($dir.'/'.$file),     
                )
            );
?>

#1

Uwe Mindrup - April 14, 2007 - 15:55

I had the same problem and I did the "cck-way" in the following manner:

<php
$node['field_bookcover'] = array  // Book-Cover
    (
        0 => array
            (
                'fid' => 'upload',
                'filename' => $imagename,
                'filepath' => $dir . '/' . $imagename,
                'filemime' => 'image/jpeg',  // the appropriate mimetype
                'filesize' => filesize($dir . '/' . $imagename)
            )
    );
?>

I put this code just before 'node_save($node)'.

#2

quicksketch - April 20, 2007 - 02:05
Status:active» duplicate

drupal_execute is the usual way to insert data in a node form programmatically, unfortunately the file handling needs some additional help. See http://drupal.org/node/112802.

#3

toxicdesign - September 21, 2007 - 01:20

@Uwe Mindrup

I dont understand how you can do it that way when $node needs->to->be->an->abject? Not array?

I'm still stumped on this...I think I'm going to just insert the data with a db_query instead of fighting with this node_save vs imagefield battle.

#4

Blackguard - March 13, 2008 - 20:55

Before you save the node, you do this:

$node = (Object)$node;

#5

voipfc - March 25, 2008 - 19:15

If you want to add more images to an existing node, how will you go about it?

#6

Blackguard - April 24, 2008 - 20:40

You could do node_load(), add the images as suggested, node_save().

#7

temaonline - April 25, 2008 - 08:11

test variant:

<?php
$node
= new StdClass();
$node->is_new = true; // if node is new
$node->uid = 1;
$node->type = 'node_with_img'; //Your content type
$node->changed = NULL;
$node->title = 'testTitle';
$node->body = 'testBody';   
$node->format = 1;
$node->log = NULL;
$node->name = 'admin';
$node->date = NULL;
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->revision = 0;
$node->preview = 'testPreview';
$node->op = 'testOp';
$node->submit = 'testSubmit';
$node->menu= Array(
   
'title'=>NULL,
   
'description'=>NULL,
   
'pid'=>1,
   
'path'=>NULL,
   
'weight'=>0,
   
'mid'=>0,
   
'type'=>6);
$node->field_img_upload = NULL;
$node->upload = NULL;
$file = '/your/path'; // Your filepath with write chmod
$node->field_img= Array(
   
0=>Array (
       
'flags'=>Array(
           
'delete'=>0
           
),
       
'alt'=>basename($file),
       
'title'=>basename($file),
       
'filename'=>basename($file),
       
'filepath'=>$file,
       
'filemime'=>'image/jpeg',
       
'filesize'=>filesize($file),
       
'fid'=>'upload',
        )
    );
$node->teaser = 'testTeaser';
$node->validated =    true;       
node_save($node);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.