0

This seems to actually be a problem in Drupal itself. In /modules/file/file.field.inc the function file_field_widget_form 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 my form programatically with drupal_form_submit, it adds in $form_state['programmed'], thus the if condition fails, $elements[0] does not exist and I get the error

Notice: Undefined offset: 0 in file_field_widget_form()

My form is submitted by a custom routine (in a custom module) that looks a bit like this:

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;   
}

drupal_form_submit("{$node->type}_node_form", $form_state, $node);

The form state before I call drupal_form_submit looks like this:

Array
(
    [values] => Array
        (
            [title] => some title
            [name] => admin
            [op] => Save
            [field_person] => 88
            [field_lead_type] => 1
            [field_lead_details] => some stuff
    )

)

What is the fix? I'd rather not mess with core modules. Is there something I can do to my own code to resolve the problem?

  • show us where you call drupal_form_submit(). Show us the $form_state your submitting. – tenken Dec 21 '12 at 6:30
  • @tenken I don't think that makes much difference as the issue will occur from anywhere drupal_form_submit is called so long as the form being submitted to has a file field in it. I updated the question anyway though. – Cameron Ball Dec 21 '12 at 7:09
0

ok, why are you not submitting a File (or FID) along with your form_state? I don't understand your problem, because I do this all the time (even with files).

Drupals file widgets usually use like Ajax to upload a file in the background and the form takes a FID in $form_state to process your forms if it has files attached.

When I asked you what the $form_state was I meant:

  1. what are you sending to drupal.
  2. what does drupal typically send as the $form_state when that form is sent through the user interface, because you're just trying to do that programmatically.

I dont think you know the answer to (2) above, because you cant get this to work right. You showed me what you're sending for (1). Here is a tutorial on using Firefox's Firebug plugin to view the submitted POST form state values for a drupal form. You want to drupal_form_submit form state to have the exact same parameters passed to it. In firebug you simply press Persist in the Net tab and you can capture URL requests.

So I'm saying goto the form in the website and submit it using your browser and Firebug to see what it expects to be passed.

You may ask how you get a FID for a file, you simply upload it using php before submitting the form and pass along its $file->fid. See this community documentation comment here how to do that.

You're just posting a simple HTTP Form, I really doubt Drupal is doing anything wrong.

  • Your advice seems incorrect. The reason I am not submitting an fid (in this particular case) is because it is an optional field. Following your advice, I added in the extra values that go with the attachment field type, but the warning persisted. Please look in the file I mentioned and you will see that the error will occur regardless of whether I add in the extra stuff in field_state. The 'real' drupal form goes through the exact same process as my programmed form except for when it reaches the aforementioned check. My form fails that check because it has been made programatically. – Cameron Ball Dec 24 '12 at 2:49
  • I should not that it does work, the node is created and everything. I just get this notice, which I think is annoying and obviously indicates either, a mistake on my part, or an oversight by who ever wrote file_field_widget_form in file.field.inc – Cameron Ball Dec 24 '12 at 3:02

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.