0

Still getting my head around Drupal 8 after using Drupal 7 for so long. I want to override (alter) the node delete action. Now in Drupal 7 I would have used node_delete_confirm_submit() and hook_node_delete() or similar such as using hook_form_alter and changing the submit function.

In Drupal 8 this has partly changed. I can still use hook_node_delete(), but there is no node_delete_confirm_submit() which was deprecated in favor of \Drupal\Core\Form\ConfirmFormBase. Rather than try and create a hack I would like to learn the proper way I should be doing this.

I have tried to use ConfirmFormBase. I have a src/Form/DeleteXXXConfirm.php file.

namespace Drupal\XXX\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

class DeleteXXXConfirm extends ConfirmFormBase {

   ... code here for confirm and delete ...

}

Since I want to override node/nid/delete, I assumed the routing file would look something like this.

XXX.delete:
  path: '/node/{id}/delete'
  defaults:
    _form: '\Drupal\XXX\Form\DeleteXXXConfirm'
    _title: 'Confirm Deletion'
  requirements:
    _permission: 'administer site configuration'
    id: ^\d+$

This doesn't appear to work, do I need a Controller? Or am I totally barking up the wrong tree? I am obviously misunderstanding something. Can anyone point me in the right direction?

  • No, in Drupal 8 you cannot define in a module a route another module is defining. That is not different from Drupal 8. The difference is that there isn't anymore hook_menu_alter(), but there is still a way to alter routes another module is providing. – kiamlaluno Feb 1 '18 at 7:59
  • @kiamlaluno thanks for the reply. You say but there is still a way to alter routes another module is providing but don't say how. So question then is how do you alter entity.node.delete_form ? – user82521 Feb 1 '18 at 9:25
1

I've found inspecting the routes availble and then working back to the controller to be a super helpful way to understand how to override things.

You will need Drupal console installed.

For example run

drupal router:debug | grep node | grep delete

This will list all routes, and then filter only ones with node in and then filter only ones with delete in.

You should then see the route you are interested in (/node/{node}/delete) is called entity.node.delete_form

Now you can debug just that specific route:

drupal router:debug entity.node.delete_form

This will show you that the form for this route is called node.delete. If however there was a method and a controller for the route is would show you exactly what the callback is.

To change the entity.node.delete_form route to use your own callback you can use the new method of doing menu_alter.

However if you want to keep the same node delete form and just change the submit handler then you can still use hook_form_alter to change this.

  • I did not in the post I have used form alter and the changed the submit handler. The issue I have is it seems to get called twice when confirming deletion and looking at what is submitted it is identical on both calls. Wouldn't be an issue but using drupal_set_message appears on the confirm page and then again after deletion. Never thought of using drupal console to trace it. Not sure why. Will give it a go and report back. Thank you Felix. – user82521 Feb 1 '18 at 7:50
  • So not figured out hook_form_alter being called twice. But making progress usin the new Drupal 8 methods work, routing and menu callbacks, etc. Once I have a working sample will put it on here. – user82521 Feb 1 '18 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy