3

I have created a form through my module in which the user(anonymous) can enter an image URL and some title for the image. The link needs to be, like with remote stream wrapper, shown as an actual image. So this means, the image should be used from an external sources and not be download on my local server.

$node = new stdClass(); // Create a new node object
$node->type = "article"; // Or page, or whatever content type you like
node_object_prepare($node);

$node->title    = filter_xss($_GET['title']);
$node->language = en; // Or e.g. 'en' if locale is enabled

$file_path = $_GET['link'];

$file = (object) array(
        'uid' => 1,
        'uri' => $file_path,
        'filemime' => file_get_mimetype($file_path),
        'status' => 1,
);

$node->field_image[$node->language][0] = (array)$file;

$node->uid = 1; // UID of the author of the node; or use $node->name

node_save($node);

I have this code and it doesn't save the image URL. I'm sending the 'link' and 'title' properties through AJAX.

1

File field references are made on the fid column in the file_managed table - you haven't saved the file object so it doesn't have a fid, and thus nothing to relate the field data to.

Just add a call to file_save after you've prepared the file object, but before you add it to the field on the node object:

file_save($file);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.