4

I want to save a image file into my custom content type using forms api and node_save().

The below code works fine but I also get the upload button as it default in managed_file .

I don't want the upload button. So I tried type=file but it did not work. How can I remove the upload button ?

Also if you just select the file and submit the form it works so I really need to just find a way to remove the upload button.

$form['image'] = array(
    '#type' => 'managed_file',
    '#title' => 'File',
    '#upload_location' => 'public://my-files/'
);
3
  • If you remove the upload button how will people choose a file to upload?
    – Clive
    Jan 21, 2012 at 13:05
  • The form type has two buttons browse and upload. Upload is only for ajax. The form works fine if you only use browse and then click submit on the form. Any idea how to get this done ?
    – vishal
    Jan 21, 2012 at 13:48
  • I do not suggest using it for multiple file attachment!
    – Ehsan
    Feb 19, 2015 at 10:21

6 Answers 6

20

The work to separate out the managed_file element is done in file_managed_file_process(), which is the default #process function for that type of element.

To hide the upload button you need to implement your own simple #process function for the element:

function MYMODULE_my_form($form, &$form_state) {
  $form['image'] = array(
    '#type' => 'managed_file',
    '#title' => 'File',
    '#upload_location' => 'public://my-files/',
    '#process' => array('MYMODULE_my_file_element_process')
  );

  return $form;
}

function MYMODULE_my_file_element_process($element, &$form_state, $form) {
  $element = file_managed_file_process($element, $form_state, $form);
  $element['upload_button']['#access'] = FALSE;
  return $element;
}
10
  • Clive you are simply the best ! I wud buy you a cup of coffee if we had done this in person. Also how did you figure this out ? Share some light on this
    – vishal
    Jan 21, 2012 at 15:28
  • 1
    Well thanks for the coffee anyway :) I just went into the core file module, looked at the file_element_info() function (implementation of hook_element_info() which is responsible for declaring form element types) and looked at the functions declared for the managed_file type to see where it was getting expanded. Then I used the devel module to find out where in the process I could hook into it and voila!
    – Clive
    Jan 21, 2012 at 15:37
  • wow I will give it a shot . this is a good learning example for me
    – vishal
    Jan 21, 2012 at 15:48
  • 2
    Have you seen the Pro Drupal Development book by any chance? If you're serious about getting into Drupal it's an absolutely fantastic resource, it really explains how Drupal works from the ground up
    – Clive
    Jan 21, 2012 at 15:50
  • thanks, I will get the book. will start reading it from tomorrow.
    – vishal
    Jan 21, 2012 at 22:12
0

@Clive code doesn't work for me

function MYMODULE_my_file_element_process($element, &$form_state, $form) {
  $element = file_managed_file_process($element, $form_state, $form);
  $element['upload_button']['#access'] = FALSE;
  return $element;
}

Invert params in the function's call does the trick

function MYMODULE_my_file_element_process($element, &$form_state, $form) {
  $element = file_managed_file_process($element, $form, &$form_state);
  $element['upload_button']['#access'] = FALSE;
  return $element;
}
0

You can try the Upload File Replace module:

This is a small utility module that automatically stops Drupal from renaming new files upload via filefield CCK. When 2 files with the same name exist, the older files will be renamed.

1
  • Welcome to Drupal Answers! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – mpdonadio
    Mar 19, 2014 at 14:44
0

Based in this tread I found the way to change the text “['#value']” in the submit button to the file upload in the web form.

Put it in the template.php of your theme and its done.

function MYTHEME_form_webform_client_form_FORMID_alter(&$form, &$form_state) {
  $form['submitted']['photo_file_photo']['#process'] = array('MYTHEME_my_file_element_process');
}

function MYTHEME_my_file_element_process($element, &$form_state, &$form) {
  $element = file_managed_file_process($element, $form_state, $form);
  //$element['upload_button']['#access'] = FALSE;
  $element['upload_button']['#value'] = 'Put me in pls';
  return $element;
}
0

In Drupal 8 this works for me: (disable ajax for the field)

$form['document'] = [
  '#title' => $this->t('Document'),
  '#type' => 'managed_file',
  '#required' => TRUE,
  '#attached' => [
    'library' => [],
  ],
];

And add this to your_theme.theme file: (hide the upload button).

/**
 * Implements template_preprocess_file_managed_file().
 */
function your_theme_preprocess_file_managed_file(&$variables) {
  switch ($variables['element']['#id']) {
    case 'edit-document':
      $variables['element']['upload_button']['#access'] = FALSE;
      break;
  }
}
-1

Adding a element process hook works but seems like overkill to me. in situations like this where its just an visual thing, CSS is the best way to go IMO since every upload button has its own id anyway.

#edit-image-upload-button{
  display:none;
}
1
  • Yes I knew about the css but I wanted to do it from the element hook . Thank you :)
    – vishal
    Jan 21, 2012 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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