drupaldrupal-7form-api

Add label-element to managed_file field with Form API


I want to style my managed_file-Field in a Drupal 7 Form-API custom form and therefore, insert a label element that will replace the "Browse for file".

So I want exactly this:

<input type="file" name="files[bild_0]">
<input type="submit" id="edit-bild-0-upload-button" value="Upload">
<label for="files[bild_0]">Choose Picture</label>

However, the Form API makes it very difficult to insert the label directly next to the input. "#prefix" and "#suffix" are not working. "#field_prefix" and "#field_suffix" inserted the html in a wrapper...any ideas?

Thank you very much in advance!


Solution

  • So, i finally figured out an alternative solution. I used the

    '#field_suffix'

    property on the Form API field to add the label element. Besides of that, I had to overwrite the initial id of the field via

    '#id' => 'upload-selector-' . $index_of_picture field

    Since the id of the actual input-field is now different from the div that contains the field, I could use the label of the field to trigger the file-dialog. I styled the label element to my needs and hid the initial input.