1

I'm having some trouble solving this issue and could do it with some help. Basically, I'm looking to replicate the "Delete (with descendants)" function from the admin panel in a front-end form.

On the front-end at the moment users can delete children or parent pages, but not the parent (and children automatically). I've tried a number of different methods but to no avail.

My front-end is using...

       <form method="post" accept-charset="UTF-8" id="delete-page">
          {{ csrfInput() }}
          {{ hiddenInput('context', 'index') }}
          {{ hiddenInput('criteria', '') }}
          {{ hiddenInput('elementAction', 'craft\\elements\\actions\\Delete') }}
          {{ hiddenInput('elementIds', entry.id) }}
          {{ hiddenInput('elementType', 'craft\\elements\\Entry') }}
          {{ hiddenInput('hard', false) }}
          {{ hiddenInput('source', "section:#{entry.section.uid}") }}
          {{ hiddenInput('withDescendants', true) }}
          <input type="submit" class="button" class="save" value="Delete P+C" accesskey="v">
      </form>

In the Craft Docs, I came across withDescendants but not sure how to utilize this on the front-end.

Any advice here?

1

Many of those advanced endpoints are undocumented, but you can find out a lot with the devtools. If you use the Delete (with Descendants) option in the backend and check the devtools, you can see it uses craft\controllers\ElementIndexesController::actionPerformAction. This method expects an elementAction parameter to tell it which action to perform. You can pass craft\\elements\\actions\\Delete which includes the withDescendants option you're looking for. The payload for the request will also tell you which parameters the action requires:

Screenshot of the payload for the perform action endpoint

Check the source code of the actionPerformAction method and the craft\elements\actions\Delete class to find which parameters you can use and which formats they can take. Then you can construct a frontend form with (hidden) inputs to perform this action for a particular element.

Adding the parameters inside a form (with method="POST") can be done using the hiddenInput Twig function:

{{ hiddenInput('elementAction', 'craft\\elements\\actions\\Delete') }}
{{ hiddenInput('elementType', 'craft\\elements\\Entry' }}
{{ hiddenInput('source', "section:#{entry.section.uid}" }}
{{ hiddenInput('withDescendants', true) }}

Check the request in the devtools as mentioned above as well as the source code for the controller endpoint and the action class to find out which parameters are required. For this action in particular, it looks like it requires criteria for the query, and an array of elementIds to delete. Some parameters may also be optional. Make sure to check the source code for dependencies between parameters as well. For example, it looks like the action will not delete any descendants if you specify hard: true to hard-delete the elements instead of soft-deleting them.

9
  • Thanks so much for the detailed response MoritzLost. That's really useful information to help me learn how to approach this. Would you have any examples of how that would be constructed? Or something along similar lines? I'm relativity new to something as complex as this. Thanks! Dec 6 '21 at 17:26
  • @KieronKeenan Basically, you need to use visible and/or hidden inputs in your form to include the data the controller requires to perform the requested action. The same way you're using {{ hiddenInput('entryId', entry.id) }} to pass the entryId to the delete-entry action, you can, for example, specify the source for the action: {{ hiddenInput('source', "section:#{entry.section.uid}") }}. Then it's just a matter of checking the request in the devtools and/or the source code to see which parameters the controller and the action require and adding those to the form.
    – MoritzLost
    Dec 7 '21 at 10:57
  • @KieronKeenan I've updated my answer with a code example.
    – MoritzLost
    Dec 7 '21 at 11:04
  • Thanks so much for the help and guidance here. I'm relatively new to Craft so this has been quite challenging for me. I'm still trying to work through the problem with your advice but still unsure what the root cause/causes that's stopping the form from deleting the parent + children entries. Dec 8 '21 at 10:07
  • 1
    Thanks @MoritzLost! I think I'm in way above my head here trying to figure this out but I really do appreciate the support & advice here. I'll keep going at it till I get it working. Dec 10 '21 at 12:10

Your Answer

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

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