Drupal - adding metadata to FileField

| | Comments (1)

While working with file uploads in Drupal, I had a situation where I needed to be able to attach metadata to FileFields. Specifically, the site needed to support video uploads, with a low-res Flash video that would be uploaded via Drupal. For each of these Flash videos, there would be a corresponding high-res broadcast video that would be hosted on an external server, and that would be made available to the user via a URL. So, I needed to be able to attach a “download URL” as metadata to each FileField. I wanted my upload form to look like this:

filefield_metadata.jpg


A few hours of Google searching led me to think that this was a pretty complex task - this page for instance is a detailed description of how to solve the problem, via the creation of a compound CCK field.

However, it turns out that there is a simpler solution, as described on the Trellon blog. Below is my description of how I solved the problem using the same technique.

The first thing you need to do is create your own Drupal module - this is very simple, and involves creating a folder in the “/sites/all/modules/” folder. My module is called “mediacentre”, so I created “/sites/all/modules/mediacentre/”, and in that folder I created two files - “mediacentre.info” and “mediacentre.module”.

Here is the “mediacentre.info” file:

    ; $Id: 
    name = Media Centre
    description = Media Centre
    core = 6.x



The “mediacentre.module” file is a little more complex:

<?php
function mediacentre_form_alter(&$form, $form_state, $form_id) {
    if ($form_id == "story_node_form") {

        // each field can have multiple values, we need to add custom process function to every upload field
        foreach (element_children($form['field_video']) as $key) {
            $type = $form['field_video'][$key]['#type'];

            if ($type == 'filefield_widget') {
                $a = array('filefield_widget_process', 'download_url_widget_process');
                $form['field_video'][$key]['#process'] = $a;

                $va = array('filefield_widget_validate', 'download_url_widget_validate');
                $form['field_video'][$key]['#element_validate'] = $va;
            }
        }
    }
}

function download_url_widget_process($element, $edit, &$form_state, $form) {
    $file = $element['#value'];

    $element['data']['download'] = array(
        '#type' => 'textfield',
        '#title' => t('Download URL'),
        '#value' => $file['fid'] ? $file['data']['download'] : ''
    );

    return $element;
}

function download_url_widget_validate($element, &$form_state) {
}


In the above code the important method is “mediacentre_form_alter” - this is a Drupal form hook - the Drupal FAPI documentation has much more information on this, most of it horribly confusing if you’re just trying - like I was - to learn the basics. With the above method in our module, every time Drupal displays a form it will call our form hook, so we can customize the form as needed.

In the form hook method we check for the ID of the form we want to modify - this can be discovered by using Firebug or the Webkit Inspector to examine your page. In my case, I wanted to modify the Drupal form for editing nodes, and I was looking specifically for the forms that were used to upload my videos. I was using CCK for these fields, and the field name was “field_video”. If you use a different field name, then you have to change the use of ‘field_video’ in the method to the name of your own field.

For each ‘field_video’, we check for the ‘filefield_widget’, which is the actual upload form we want to modify, and we replace the existing ‘#process’ key for that array with a new value that includes the name of our “process” or “validate” method.

So, when Drupal displays the “filefield_widget”, it will now call our “download_url_widget_process” method, and that method will insert a new element into the form with the name ‘download’, and the title ‘Download URL’.

Right now we’re not doing any particular validation, so we just leave the “download_url_widget_validate” method blank. Normally this method would contain code to make sure that the value entered is OK.

And that’s it! When displaying your data in a tpl.php you have access to the new “download” value, in the same way you would access the “description” value, as a key in the item->data array.

1 Comments

Hi Bill,

I copied your code and It works until you click on "add another" filefield item. At that point the extra information disapears. Do you have a solution for that?

I posted in http://www.trellon.com/content/blog/adding-new-fields-to-file-uploads#comment-7555 as well

thanks

Leave a comment

About this Entry

This page contains a single entry by Bill Hutten published on February 8, 2010 10:28 AM.

How to manually customize Firebug 1.5.0 for widescreen use was the previous entry in this blog.

Using the 'ref' option in ExtJS 3.x is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.34-en