I have a simple routine which I use to create nodes. The logic is something like this:

function make_node($node_type, $field_data) {
    foreach ($field_data as $k => $detail) {
        $field_key = _get_field_keys('field_' . $k);
        $field_key = $field_key[0];

        $node -> {'field_' . $k}[$node -> language][0][$field_key] = $detail;
        $form_state['values']['field_' . $k] = $detail;   

Then I run drupal_form_submit and it returns any errors from that and everything is great.

I'm having an issue with creating nodes which have a file upload field which uses the file u widget. I get the error:

Notice: Undefined offset: 0 in file_field_widget_form()

I had a look at the offending line (line 511 of modules/file/file.field.inc) and I can see why it occurs, the function file_field_widget_form has an argument called $element, which is supposed to have an index 0, but when I set up the form programatically, that index is not set up.

When I make a node of the same type using the web interface, I can see that the $elements array is set up with the 0 index before even submitting the form.

How do I rectify this issue, do I need to call something else before making my form programatically? I'm using Drupal 7 if it helps.


Looks like this is actually a problem in Drupal itself. In modules/file/file.field.inc the function field_form_get_state has a line like this near the bottom (just before the return statement):

$elements['#file_upload_description'] = theme('file_upload_help', array('description' => '', 'upload_validators' => $elements[0]['#upload_validators']));

The issue is a small if condition a few lines back which looks like this:

if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $field['cardinality']) && empty($form_state['programmed'])) {
    $elements[$delta] = $element;
    some more stuff

Since I am submitting the form programmatically with drupal_form_submit(), it adds in $form_state['programmed']; thus, the control statement's condition fails, and $elements[0] does not exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.