Ann Arbor https://www.zengenuity.com/ en How to Import Files into Drupal CCK Fields https://www.zengenuity.com/blog/2010-11/how-import-files-drupal-cck-fields <span class="field field--name-title field--type-string field--label-hidden">How to Import Files into Drupal CCK Fields</span> <div class="paragraph html"> <div class="container"> <p>At last night's <a href="https://groups.drupal.org/ann-arbor">Ann Arbor Drupal Meetup</a>, a question was raised about how to write an import script to create <a href="https://drupal.org">Drupal</a> nodes that include a <a href="https://drupal.org/project/filefield">Filefield CCK</a> field. Filefields and <a href="https://drupal.org/project/imagefield">Imagefields</a> present a particular problem for import because the file must be stored into the Drupal files table before you can save them to the node. In the discussion that followed, it seemed that several of us at the meeting had developed our own ways to do this in the past for various reasons. So, I thought I would post a couple solutions for others who need to do this.</p> <h2>Option 1: Read the Manual</h2> <p>There is <a href="https://drupal.org/node/330421">a manual page that explains how to programmatically import files to Filefield</a>. However, like many Drupal manual pages, if you read through the comments, there are about 20 different versions of the code, and the discussion is often difficult for novice users to follow. The code on this page does work, but there is something simpler that we ended up with at the meeting.</p> <h2>Option 2: Use a Helper Function</h2> <p>The Filefield module has a helper function called <a href="https://api.lullabot.com/field_file_save_file">field_file_save_file()</a> that does a lot of the work for you. To use the function, you just need to give the original file path and a destination path if you want the file copied into a new location. Here's a simple example of a script that creates a story node and imports an image file into the "field_image" CCK field. (The file importing lines are highlighted.)</p> <pre><pre class="[8,9]'"> <?php $node = new StdClass(); $node->type = 'story'; $node->body = 'Testing.'; $node->title = 'Testing'; $file = field_file_save_file('import-images/buzz2.jpg', array(), 'sites/default/files/images'); $node->field_image = array($file); $node->uid = 1; $node->status = 1; node_save($node); </pre></pre> <p>This script can't be run on the command line without some other include lines to bootstrap the Drupal system. However, if you use <a href="https://drupal.org/project/drush">Drush</a> (and you should be using Drush), you don't need to do anything extra. Just run "<span><strong><span>drush scr import.php</span></strong></span>". That's it. Adding those two lines to your import script is all need to do to import files to Filefield CCK fields. Easy to do now that you know.</p> <p>If you have any questions or problems using the method above, leave a comment below, and I'll see if I can help you out.</p> </div> </div> <span>Wayne Eaker</span>November 30, 2010 <div class="tags"> <div class="container"> <span class="tag"><a href="https://www.zengenuity.com/blog/tags/ann-arbor" hreflang="en">Ann Arbor</a></span> <span class="tag"><a href="https://www.zengenuity.com/blog/tags/drupal" hreflang="en">Drupal</a></span> <span class="tag"><a href="https://www.zengenuity.com/blog/tags/drupal-planet" hreflang="en">Drupal Planet</a></span> <span class="tag"><a href="https://www.zengenuity.com/blog/tags/filefield-module" hreflang="en">Filefield module</a></span> <span class="tag"><a href="https://www.zengenuity.com/blog/tags/imagefield-module" hreflang="en">Imagefield module</a></span> <span class="tag"><a href="https://www.zengenuity.com/blog/tags/import" hreflang="en">Import</a></span> </div> </div> Tue, 30 Nov 2010 15:28:12 +0000 Wayne Eaker 120 at https://www.zengenuity.com