Is it possible to add an image to a node programmatically?
相关问题
- Drupal 8: How do I customize a form widget to show
- How to balance webserver bandwith usage?
- drupal :: order complete hook and upgrade user per
- Change redirect in Drupal 7 for Password Recovery
- Adding attachment to Jira's api
相关文章
- Render a Drupal node
- How to allow multiple blocks in a module of Drupal
- How can I redirect a Drupal user after they create
- Drupal 6: Getting custom fields into the database
- Drupal 7 retain file upload
- Drupal - Set 'user/%/edit/uprofile' to def
- Drupal Session User id for independent script
- how to construct an https POST request with drupal
An easier way:
This is what worked for me:
Just going to paste my solution here as well, I needed to create a new node, and upload an image programmatically.
Here's one extra bit that tripped me up for a while: this will attach the image to the node, and if you're adding the image then you're okay. However, if you're updating an image, and you care about displaying it on a page, then one extra step is needed before calling node_save():
This will regenerate all of that image's styles.
$node->field_image[LANGUAGE_NONE][0] = (array)$file;
I tried this with a multilingual site. It failed fairly... but horribly. I had to specify the language in question. Simply put, this worked instead:
$node->field_image['en'][0] = (array)$file;
Without it, the attached file was viewable in the 'view' screen but not in the 'edit' screen.
Yes, make it part of the $node object when you save it. Save it using node_save().