Jump to content

Prevent Page to be deleted when it is referenced by another page


KarlvonKarton
 Share

Recommended Posts

Try (untested)

$myCategoryPage = $pages->get('/path/to/an/example/category/page/);
// if the category page is not referenced in a page reference field
if(!$myCategoryPage->numReferences) {
 // safe to delete
	$pages->trash($myCategoryPage);
}

http://processwire.com/api/ref/page/

ps: if it doesn't work, try $myCategoryPage->numReferencing instead. I get confused which is which :-). Please report which one worked for you, thanks.

  • Like 2
Link to comment
Share on other sites

8 minutes ago, KarlvonKarton said:

Maybe I should have specified that, but I mean in admin (not api).  ?

Then you have to hook into something. If you want that referenced categories not to display the trash link, then you can hook into the tree render. Or, if you don't mind the display of the trash page link, then you can hook before pages::deleteReady. Alternatively, depending on who is administering the site, you can lock all your category pages and only unlock one you want to delete, at delete time :-). You can check if it is being referenced in a page field by going to the settings tab when editing the category page and scroll to 'What pages point to this one' section.

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Given the client situation I think it's best to inform them to be careful with the categories (for now)...
And maybe later I will go for the hook option (it would be my first hook actually - no more escapism ? ).

IMHO: I think that this should be some core logic not to be able to delete pages that are referenced by other pages via the PageField.  Or at least get a notice that there are still references to it.  It feels odd to me like this.  

  • Like 1
Link to comment
Share on other sites

13 hours ago, KarlvonKarton said:

IMHO: I think that this should be some core logic not to be able to delete pages that are referenced by other pages via the PageField.  Or at least get a notice that there are still references to it.  It feels odd to me like this.  

Makes sense. File a feature request ?

Link to comment
Share on other sites

9 minutes ago, kongondo said:

Makes sense. File a feature request ?

It is also discussed in the issue I likened to. Eg.:

"I guess at the minimum, the system should ask if the user really wants to trash the page as it is being referenced at least once "somewhere". It should also point out where the reference(s) are and what to expect if they decide to trash it..."

Do you think it is necessary to open another issue?

Link to comment
Share on other sites

15 hours ago, KarlvonKarton said:

And maybe later I will go for the hook option (it would be my first hook actually - no more escapism ? ).

It's linked to in the GitHub issue that szabesz mentioned, but in case you missed it, there is a hook here for preventing referenced pages from being trashed...

...and from being trashed or directly deleted...

 

  • Like 2
Link to comment
Share on other sites

2 hours ago, szabesz said:

It is also discussed in the issue I likened to. Eg.:

"I guess at the minimum, the system should ask if the user really wants to trash the page as it is being referenced at least once "somewhere". It should also point out where the reference(s) are and what to expect if they decide to trash it..."

Do you think it is necessary to open another issue?

I didn't read that, sorry. Now, that I have, the discussion you are referring to, whilst related, is somewhat different, I think. That discussion was a bug/issue report. The issue is about Page Reference fields themselves, behaving in an unusual way...etc. The present issue, IMHO, is a not about the behaviour of page reference fields, but about pages referenced in those fields, specifically, whether they should be deletable or not. I think the present issue is not a bug report but a feature request. Namely, similar to locked items in the tree and in page edit, a trash icon/delete button(or link) should not be visible for referenced pages. In the API, one should not be able to delete such pages as well, unless they pass some flag...(my thought).

What do you think?

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...