3

I have a handful of file fields that have the following default Drupal behavior:

  • User can upload a file
  • User can upload another file in place
  • User can remove file

Standard config. Whats happening is the user wants the file being replaced to be deleted before the new one is uploaded, and thus, avoiding the issue where files have their names replaced with _N identifiers to ensure unique filenames.

This is because they are referencing physical file paths from the menu and WYSIWYG fields - which means the old replaced file is being referenced instead of the new one. Since these are core file fields and over 100 files have been uploaded, I cannot switch out to a Media field. I'm also not sure if that would even solve the problem at hand, anyway.

Is there any way I can hook into this widget via form alter, detect a click on the upload field, and if a new file is being added, delete the old one in place so that the file being uploaded the name stays intact and thus preserving link targets?

I have looked at the form with both hook_form_alter and hook_field_widget_form_alter - but I am not really seeing anything I can use to hook into.

10
  • We can rename the file using hook_file_insert(Drupal file FileInterface $file).
    – developer
    Commented Mar 21, 2017 at 5:28
  • I would add submit handler to the file widget/element that would, after removing usage, physically delete the file.
    – user21641
    Commented Mar 21, 2017 at 7:14
  • hook_file_insert would be too late. Need to delete it when Remove is clicked.
    – Kevin
    Commented Mar 21, 2017 at 13:18
  • @Ivan where are you seeing this?
    – Kevin
    Commented Mar 21, 2017 at 13:37
  • 1
    Reacting to the "Remove" being clicked is to early. If the user does not commit to the action (click save) you have now lost the file. This does feel like the time for a custom submit handler. Perform the file_copy() yourself and set the $replace parameter to FILE_EXISTS_REPLACE.
    – Gold
    Commented Jan 24, 2018 at 21:15

1 Answer 1

1

You can use this approach.

/**
 * Implements hook_field_widget_form_alter().
 */
function insights_analyst_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
    if($element['#field_name'] == 'field_figure'){
        $element['#process'][] = 'removeFile';
    }
}

function removeFile(&$element , \Drupal\Core\Form\FormStateInterface $form_state, &$complete_form){
    $element['remove_button']['#submit'][] = 'markupRemove';
    return $element;
}

function markupRemove($form , \Drupal\Core\Form\FormStateInterface $form_state){
    $parents = $form_state->getTriggeringElement()['#array_parents'];
    $button_key = array_pop($parents);
    //print_r($button_key);
    if($button_key == 'remove_button'){
       //Write your logic here
    }
}

Your Answer

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

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