5

Does managed file type in drupal 7 form api work with #prefix and *#sufix * properties?

$form['myfilefield'] = array(
            '#title' => t('Image'),
            '#type' => 'managed_file',
            '#title_display' => 'invisible',
            '#id'=>'edit-image-example-image-fid',
            '#prefix' => '<div class="myDiv">',
            '#sufix' => '</div>',
            '#default_value' => variable_get('myfilefield', ''),
            '#upload_location' => 'public://myimages/',
);
5
  • Have you tried it? – Clive Nov 6 '12 at 9:22
  • 1
    Yes!. i have tried it is not working – Ravindra Singh Nov 6 '12 at 9:32
  • According the the documentation it is supported, yes – Clive Nov 6 '12 at 9:36
  • 1
    yes. According the the documentation it is supported....... – Ravindra Singh Nov 6 '12 at 9:52
  • 1
    But while using it does not work – Ravindra Singh Nov 11 '12 at 4:14
7

Yea..all though drupal 7 form api for managed_file shows #prefix/#suffix is supported, it did not work for me either.
So I tried using #field_prefix and #field_suffix and it worked. :)
I know this is a old post but I am posting answer hoping it will help someone. :->

5

As of this posting, no it does not. file_managed_file_process() in modules/file/file.module clobbers whatever you set. See line 470:

// Prefix and suffix used for Ajax replacement.
$element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
$element['#suffix'] = '</div>';

This feels like a bug; I've posted an issue at http://drupal.org/node/1918630.

#field_prefix and #field_suffix can work but it depends on what you're trying to do. If you need to wrap the file_managed in a table cell, for example, you may end up having to put it inside of an empty container to get fully outside of this element's <div>:

$array['rows'][] = array(
  'file_managed_container' => array(
    '#prefix' => '<td>',
    '#suffix' => '</td>',
    'file_managed_element' => array(
      '#type' => 'managed_file',
      ...
    ),
  ),
);
3
  • I just submitted a very simple patch for the file.module on your issue. Basically I just check for an existing prefix/suffix and then wrap the new prefix/suffix with those. You may want to test it when you get a chance to see if it if works for you. – David Stinemetze Sep 28 '15 at 16:56
  • Thank you for posting a patch. I don't think this will ever be changed because D7 matured many years ago and existing code probably assumes this behavior. I thought it was unfortunate that core clobbered its own property that way. – Charlie Schliesser Sep 28 '15 at 17:09
  • 1
    Well probably. I know everyone is distracted by D8, but figured I'd post the patch anyway in hopes maybe someone will see how straightforward the patch is, and just lump it in with future security updates or something. Doubtful, but worth a shot. – David Stinemetze Sep 28 '15 at 17:26

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.