In Drupal 6 I created a simple module that disabled the checkbox for deleting attachments, as I do not want attachments removed, only uploaded.

In Drupal 7, however I can not figure out how to do the same, disable the "Remove" button on attachments.

I can tell you I used hook_form_alter() to disable the checkboxes by adding #disabled = TRUE attribute, and hook_form_validate() to check $form_state['values'] to prevent attachment removal via DOM submissions.

Any help is good help and is greatly appreciated.

Here is what I have got so far in Drupal 7 code, does not throw errors, but does not disable the attachment "Remove" button.

/**
 * Implements hook_form_alter().
 */
function disable_delete_attachments_form_alter(&$form, $form_state, $form_id) {
  // Ugly debugging:
  //print_r($form_state);
  //print_r($form);

  if (!user_access('Delete Attachments')) {
    if (isset($form['upload']['und'])) {
      foreach ($form['upload']['und'] as $key => $value) {
        if (is_numeric($key)) {
          // Can't locate the button to change the attributes
          // $form['upload']['und'][$key]['remove_button']['#disabled'] = TRUE;

          //$element['remove_button']['#access'] = FALSE;
          //$element['remove_button']['#disabled'] = TRUE;

          $form['#validate'][] = 'disable_delete_attachments_form_validate';
          //$form['#submit'][] = 'disable_delete_attachments_form_submit';
        }
      }
    }
  }
}

Here is my *working* Drupal 6 code

/**
 * Implements hook_form_alter().
 */
function disable_delete_attachments_form_alter(&$form, $form_state, $form_id) {
  if (!user_access('Delete Uploads') && user_access('upload files')) {
    if (isset($form['attachments']['wrapper']['files'])) {
      foreach ($form['attachments']['wrapper']['files'] as $key => $value) {
        if (is_numeric($key)) {
          $form['attachments']['wrapper']['files'][$key]['remove']['#disabled'] = TRUE;

          $form['#validate'][] = 'disable_delete_attachments_form_validate';
          //$form['#submit'][] = 'disable_delete_attachments_form_submit';
        }
      }
    }
  }
}


/**
 * A FAPI validate handler.
 * Prevents attachments from being removed through DOM submissions if the user does not
 * have appropriate access
 */
function disable_delete_attachments_form_validate($form, &$form_state) {
  if (!user_access('Delete Uploads') && user_access('upload files')) {
    if (isset($form_state['values']['files'])) {
      foreach ($form_state['values']['files'] as $key => $value) {
        if (is_numeric($key)) {
          if (isset($form_state['values']['files'][$key])) {
            if ($form_state['values']['files'][$key]['remove'] != '') {
              form_set_error(
                'files[' . $key . '][remove]',
                t(
                  'Form validation could not be completed when reviewing attachment removal permissions.',
                  array('%name' => $form_state['values']['files'][$key]['remove'])
                )
              );
            }
          }
        }
      }
    }
  }
}

Comments

linwiz’s picture

I've added the D6 code and what I have done so far on D7.

linwiz’s picture

can no one tell me where I can access the "Remove" button attributes for file attachments on the node edit form?

linwiz’s picture

Can anyone tell me where I could find the information I'm looking for? I can't find it, I've tried.

linwiz’s picture

I can't believe that nobody in this community knows anything about what I am asking for. Please help me

pavelmsokolov’s picture

Similar problem is described here: https://www.drupal.org/node/1726960

Also, you can use CSS for this.

In a hook_form_alter() you can append a new CSS class to file field:

function your_module_form_alter(&$form, &$form_state, $form_id) {
$form['field_attachment']['#attributes']['class'][] = 'hide-remove-button';

Then, in .css file you could do the following:

.hide-remove-button #edit-field-attachment-und-0-remove-button {
  display: none;
}