over 5 years ago
Hi All,
It is quite easy to add a node programmatically in Drupal 7. But it gets little bit complex when we need to add an image to this node as well.
Use the following codebase to add an image to a node programmatically:
global $user_id; $filename = 'myimage.jpg'; $image_object = file_get_contents('url_of_your_image); $file = file_save_data($image_object, 'public://' . $filename, FILE_EXISTS_RENAME); $file->uid = $user->uid; $node->field_my_image[$node->language][0] = (array)$file; node_save($node); //If you want to save and existing image from drupal directory $filepath = drupal_realpath('misc/druplicon.png'); // Create managed File object and associate with Image field. $file = (object) array( 'uid' => 1, 'uri' => $filepath, 'filemime' => file_get_mimetype($filepath), 'status' => 1, ); // We save the file to the root of the files directory. $file = file_copy($file, 'public://'); $node->field_my_image[$node->language][0] = (array)$file; node_save($node);
Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).
Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.
Chrome Version Support
Are you sure, you want to delete this comment?
Sign up using
0 Comment(s)