tags:

views:

354

answers:

5

I'm using Drupal 6.x and I'm writing a php class that will grab an RSS feed and insert it as nodes.

The problem I'm having is that the RSS feed comes with images, and I cannot figure out how to insert them properly.

This page has some information but it doesn't work.

I create and save the image to the server, and then I add it to the node object, but when the node object is saved it has no image object.

This creates the image object:

$image = array();
if($first['image'] != '' || $first['image'] != null){
 $imgName = urlencode('a' . crypt($first['image'])) . ".jpg";
 $fullName = dirname(__FILE__) . '/../../sites/default/files/rssImg/a' . $imgName;
 $uri = 'sites/default/files/rssImg/' . $imgName;

 save_image($first['image'], $fullName);

 $imageNode = array(
  "fid" => 'upload',
  "uid" => 1,
  "filename" => $imgName,
  "filepath" => $uri,
  "filemime" => "image/jpeg",
  "status" => 1,
  'filesize' => filesize($fullName),
  'timestamp' => time(),
  'view' => '<img class="imagefield imagefield-field_images" alt="' . $first['title'] . '" src="/' . $uri . '" /> ',
 );
 $image = $imageNode;
}

and this adds the image to the node:

$node->field_images[] = $image;

I save the node using

module_load_include('inc', 'node', 'node.pages');

// Finally, save the node
node_save(&$node);

but when I dump the node object, it no longer as the image. I've checked the database, and an image does show up in the table, but it has no fid. How can I do this?

A: 

If you read the comments on the site you posted, you'll find:

To attach a file in a "File" field (i.e. the field type supplied by FileField module) you have to add a "description" item to the field array, like this:

$node->field_file = array(
    array(
    'fid' => 'upload',
    'title' => basename($file_temp),
    'filename' => basename($file_temp),
    'filepath' => $file_temp,
    'filesize' => filesize($file_temp),
    'list' => 1, // always list
    'filemime' => mimedetect_mime($file_temp),
    'description' =>  basename($file_temp),// <-- add this
    ), );

maybe that works. You could also try to begin the assignment that way:

$node->field_file = array(0 => array(...
schneck
That was pertaining only to the FileField module, which I'm not using, this is CCK content.
Malfist
FileField is a CCK extention
Jeremy French
+2  A: 

Why not get the Feedapi, along with feedapi mapper, and map the image from the RSS field into an image CCK field you create for whatever kind of node you're using?

It's very easy, and you can even do it with a user interface.

Just enable feedapi, feedapi node (not sure on the exact name of that module), feedapi mapper, and create a feed.

Once you've created the feed, go to map (which you can do for the feed content type in general as well) on the feed node, and select which feed items will go to which node fields.

You'll want to delete your feed items at this point if they've already been created, and then refresh the feed. That should be all you need to do.

Do you need to create a separate module, or would an existing one work for you?

Are you using hook_nodeapi to add the image object to the node object when $op = 'prepare'?

msumme
A: 

If you are just grabbing content wich has images in, why not just in the body of your nodes change the path of the images to absolute rather than local, then you won't have to worry about CCK at all.

Jeremy French
the way the images are being used, it won't work that way.
Malfist
A: 

Two other things that you may need to do

  1. "fid" => 'upload', change this to the fid of the file when you save it. If it isn't in the files table then that is something you will need to fix. I think you will need feild_file_save_file()

    2.you may need an array "data" under your imageNode array with "title", "description" and "alt" as fields.

Jeremy French
A: 

To build file information, if you have filefield module, you can also use field_file_load() or field_file_save_file(). See file filefield/field_file.inc for details.

Example: $node->field_name[] = field_file_load('sites/default/files/filename.gif');

onequad