How to add a file to a field of type archive with custom code in Drupal 7

Mon, 12/31/2012 - 10:45 | 113 Views

Hello everyone

In some occasions you need to replace or attach new files to nodos who has a field of type archive, using a control default or well an image as it shows.


This represent a bigger problem if there are few images, cause we can use the administrative interface, but if we don’t face the migration process, the measure of images can be considerable and make it in a and-operated way it’s not an option anymore. Obviously the first thing that comes up to our mind is to do it in a code path, but this process can be a little bit difficult, cause the files have to be in a files entity in Drupal first to can connect it with a nodo. Hereafter I’m going to show you an example code that keep to the objective.

$assets = array( 1 => 'casa.jpg', 2 => 'villa.jpg', 3 => 'hotel.jpeg');
  foreach ($assets as $nid => $asset) {
    $source = new stdClass();
    $source->uri = file_stream_wrapper_uri_normalize(DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_module') . '/images/' . $asset);
    $source->filename = $asset;
    $source->name = pathinfo($source->filename, PATHINFO_FILENAME);
    $dest = 'public://' . $asset;
    $file = file_copy($source, $dest);

    $property = node_load($nid);
    $property->field_imagen[LANGUAGE_NONE][0] = array(
      'fid' => $file->fid,
      'display' => 1,
      'description' => '',
      'uid' => $property->uid,
      'filename' => $file->filename,
      'uri' => $file->uri,
      'filemime' => $file->filemime,
      'filesize' => $file->filesize,
      'status' => $file->status,
      'timestamp' => $file->timestamp,

    // Save new node.

Now I’m going to explain the logic.

First, we created a value vector where the key is the nodo identifier, which we are going to upgrade and the value is the name of the file that you want to attach to the nodo. Create a cycle to process every image, first you have to define an object for the original image, with the properties that Drupal expects from a file, in the code we have to indicate which is the path for the images, whom is assume that they are into a carpet of a migration module called migrate_module. Then you have to define the destiny using the identifier public:// which Drupal will traduce to the right value define as the public carpet named /admin/config/media/file-system.

To make thing easier we use the Drupal function file_copy, this function will copy the file to the destination and make the operation for create a register from a file type inside Drupal and it returns a file object that can be used as a reference in any Drupal entity. Assuming that we have a content type called property and inside of that, there’s a field of image type so you proceed to link it as the first image.

The last step is use the function node_save, to save all the reference in the nodo, so the image will be linked to the nodo in a permanent way. 

Talk to you soon!.