Jump to content

Custom delete page in edit form


MaierGrem
 Share

Recommended Posts

How can I make a custom page deletion?

I made the checkbox delete, the confirmation field delete_confirm, into which I need to enter the word “DELETE” and the text field to describe the reason for the removal ..

The form has the required fields first, middle, last name.

I tried different hooks (after &before pages::saveReady, save, saved e.t.c), which checks the delete, delete_confirm fields, create notify in session and redirect to the root in the admin panel. All used hooks gave the same result.

The page is deleted, but warnings are displayed that the fields first, middle, last name is required .. 

How to intercept events and which hook is better to use to implement such deletion?

Link to comment
Share on other sites

First of all I'd test it without mandatory fields (first, middle, last name) to ensure everything else is working as expected.

If that's the case I'd start with one mandatory field to check it's behaviour. Never done a similar thing in the past - so it's just a recommendation or workflow I'd try.

Link to comment
Share on other sites

@wbmnfktr, thanks for answer!

i'll check this functionality without required fields, and all work.

I can not figure out at what point the system generates notifications of mandatory fields. With the standard function of moving to the basket, fields are not checked. And when removed from the basket too.

Link to comment
Share on other sites

Hard to tell for me as well. 

Maybe you could post some of your code here and tell us more about the overall setup. And details like ProcessWire version, the hook(s) you use, where they are placed (ready.php, a module, ...).

That would help us to give more help and tips.

Link to comment
Share on other sites

@wbmnfktr, okey, here is my code in ready.php: 

$this->addHookAfter('Pages::save', null, 'deleteUserAccount');

function deleteUserAccount(HookEvent $event) {
	// bd('deleteUser');
	$page = $event->arguments('page');
	$template = $page->template;
	$profiles = [ 'member', 'editor', 'administrator', 'supervisor' ];

	if (in_array($template, $profiles)) {
		if ($page->delete && $page->delete_confirm === 'DELETE') {
			$user = wire('users')->get($page->user_id);
			if ((bool) $user->id) wire('users')->delete($user);
			$message  = "Profile and user deleted.";
			wire('notices')->add(new NoticeMessage($message));
			wire('pages')->delete($page);
			wire('session')->redirect('/admin');
		}
	}

}

ProcessWire version 3.0.139.

I tried the hooks:

  • After/Before Pages::saveReady
  • After Pages::save
  • After Pages::saved

After redirect everywhere notifications of required fields appear. And this is logical, because the page is then saved without data. When there is data, then everything works as it should. But if the profile was created by mistake, and you need to delete it without filling in the fields, then you do not need to show notifications.

Link to comment
Share on other sites

You have to hook before the input is processed and make those fields not required.

Example:

$wire->addHookBefore('ProcessPageEdit::processInput', function(HookEvent $event) {
	/* @var InputfieldWrapper $form */
	$form = $event->arguments(0);

	// Only for the ProcessPageEdit form
	if($form->name !== 'ProcessPageEdit') return;

	$page = $event->object->getPage();
	// Use $page to check for specific templates here...

	// Check field values in POST
	$input = $event->wire('input');
	if($input->post->delete && $input->post->delete_confirm === 'DELETE') {
		// Get normally required fields and make them not required
		$first_name = $form->getChildByName('first_name');
		$first_name->required = false;
		// Repeat for other required fields...

		// Do your delete/notification/redirect actions here
		// Or if you need the page to actually save when deleting then do actions in separate Pages::saved hook or similar
	}
});

 

Edited by Robin S
Might want to run actions in separate hook if page needs to save
  • Like 3
  • Thanks 1
Link to comment
Share on other sites

@Robin S - thanks for answer! It's work!

Solution:

1. Hook before ProcessPageEdit::processInput in which I make fields not required.

2. Hook after Pages::save in which I remove page (profile) and user.

But there are a couple of points:

1. In ProcessPageEdit::processInput hook, accessing wire('pages')->delete($page) does not work.

2. How can I get all the fields in the $form, so that through the function each make them not required?

3. Is it possible to somehow combine this into one hook? Could it be calling Pages::save in ProcessPageEdit?

Link to comment
Share on other sites

6 hours ago, MaierGrem said:

1. In ProcessPageEdit::processInput hook, accessing wire('pages')->delete($page) does not work.

Not why it wouldn't work for you - it works for me. Tracy Debugger is a useful tool that can help you debug.

6 hours ago, MaierGrem said:

2. How can I get all the fields in the $form, so that through the function each make them not required?

3. Is it possible to somehow combine this into one hook? Could it be calling Pages::save in ProcessPageEdit?

Now that I see you are deleting the page being saved you don't need to worry about making the fields not required - you can delete the page and redirect all within the before ProcessPageEdit::processInput hook. When you redirect you interrupt anything else that would normally happen such as evaluating the input for empty required fields and saving the page so these things will not happen.

$wire->addHookBefore('ProcessPageEdit::processInput', function(HookEvent $event) {
	/* @var InputfieldWrapper $form */
	$form = $event->arguments(0);

	// Only for the ProcessPageEdit form
	if($form->name !== 'ProcessPageEdit') return;

	$page = $event->object->getPage();
	// Use $page to check for specific templates here...

	// Check field values in POST
	$input = $event->wire('input');
	if($input->post->delete && $input->post->delete_confirm === 'DELETE') {
		$event->wire('pages')->delete($page); // Delete user also if needed
		$event->message('Your message here');
		$event->wire('session')->redirect($event->wire('config')->urls->admin);
	}
});

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...