0

I'm working on a D8 project and in many cases I use file and image upload fields.

The files and the images are visible above the field and the user can delete them by using the default 'Remove' button.

What I want to do, is to add a confirmation popup asking if they really want to delete that files/images.

Is there any hook that I could use to make that check? If I use javascript, how can I say to drupal that now can proceed to deletion?

Thank you in advance.

EDIT:

I tried to do what @Ivan suggested but I can't make it work. My form has submit and ajax callback. Should I alter both?

Is there any working example? I am so desperate!!

2
  • 1
    You can alter the image/file widget(hook_field_widget_form_alter) and so that when the button is pressed a modal dialog is returned as ajax response instead of calling the delete method right away.
    – user21641
    Jul 17, 2017 at 18:44
  • Thanks. Should I overwrite the existing delete method and call it on my response function?
    – Ismini
    Jul 18, 2017 at 12:17

1 Answer 1

0

After long time I managed to find the solution. I didn't manage to make it work server side with modals so I managed to solve it using jQuery.

So I created a javascript file:

    Drupal.behaviors.confirm = {
        attach: function(context, settings) {

            $.each($('.form-submit'), function(){
                var str = $(this).attr('id');
                //find the id for each button
                var remove_btn_id = '#' + $(this).attr('id');

                //apply only to remove buttons and only to those that the functionality is not applied yet. 
                if (str.indexOf("-remove-button") >= 0 && !$(remove_btn_id).hasClass('custom-handler')){

                    //Drupal.behaviours are triggered every time my ajax was submitted and I ended up with multiple events to each button. 
                    //In order to avoid it, add a class to check if the button already has that functionality. 
                    $(remove_btn_id).addClass('custom-handler');
                    $(remove_btn_id).bindFirst('mousedown', function(e, next) {

                       //Display popup and get user's decision.
                        var choice = confirm('Are you sure you want to delete that?');
                        if(!choice){
                            //if user clicks cancel then do nothing
                            e.stopImmediatePropagation();
                        }
                    });
                }
            });
        }
    }

I'm not sure if this the right way to implement that functionality (I guess is not the Drupal way) but at leat it works!

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.