1

In a content type I have an image field. For the upload I use Multiupload Imagefield Widget. So now it is easy to upload 10 images into one field of the content type.

If I want to remove this 10 images, I have to click remove button 10 times. Is there a module which provides a single remove button to remove all images in this field on one click?

0

2 Answers 2

0
<?php
/**
 * Implements hook_field_widget_form_alter().
 */
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  $found = FALSE;

  if (isset($element['#field_name'])) {
    $details = $element;
    $found = TRUE;
  }
  else {
    foreach ($element as $key => $value) {
      if (isset($value['#field_name'])) {
        $found = TRUE;
        $details = $value;
        break;
      }
    }
  }

  if ($found && variable_get('MYMODULE_' . $details['#field_name'], 0) == 1) {
    $remove_button = array(
      '#type' => 'button',
      '#value' => t('Remove All'),
      '#ajax' => array(
        'event' => 'click',
        'callback' => 'MYMODULE_fields_callback',
        'wrapper' => 'checkboxes-div',
        'method' => 'replace',
       ),
    );
    $element['remove_button'] = $remove_button;
  }
}

function MYMODULE_fields_callback($form, &$form_state) {
  $new_var = $form_state['build_info']['args'][0];
  $nid = $new_var->nid;
  if(is_numeric($nid)){

    // get all fid's attached to this node
    $get_fids = db_select('file_usage', 'fu')
    ->fields('fu')
    ->condition('fu.id', $nid, '=')
    ->execute()
    ->fetchAllKeyed(0,1);

    foreach ($get_fids as $key => $value) {
      $fids[] = $key;
    }
  }

  // delete all files
  foreach ($fids as $key => $val) {
    $file = file_load($val);
    // file_usage_delete($file, 'MYMODULE');
    file_delete($file, $force = TRUE);
  }
}

check this .it is taken from others posted query hopefully its help for you

0

Refer to the question about "Delete multiple images at once from one node in Drupal 7", which appears to be similar to yours.

You may also want to look at the article about Drupal 7 Unlimited Fields Remove Button, from where you can also download the Unlimited Fields Remove module.

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