KarlvonKarton Posted September 27, 2018 Share Posted September 27, 2018 Hi, I'm having projects pages and categories pages. Each project has a PageField that references to one or more category/ies. How can I prevent that a category is deleted/trashed when a project still references it? Thx. Link to comment Share on other sites More sharing options...
kongondo Posted September 27, 2018 Share Posted September 27, 2018 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. 2 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted September 27, 2018 Author Share Posted September 27, 2018 4 minutes ago, kongondo said: Please report which one worked for you, thanks. Maybe I should have specified that, but I mean in admin (not api). ? Now I can just click on trash on a category and even when still referenced it is trashed immediately. (I don't want that ? ) Link to comment Share on other sites More sharing options...
kongondo Posted September 27, 2018 Share Posted September 27, 2018 (edited) 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 September 27, 2018 by kongondo 2 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted September 27, 2018 Author Share Posted September 27, 2018 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. 1 Link to comment Share on other sites More sharing options...
szabesz Posted September 27, 2018 Share Posted September 27, 2018 related issue at github: https://github.com/processwire/processwire-issues/issues/620 1 Link to comment Share on other sites More sharing options...
szabesz Posted September 27, 2018 Share Posted September 27, 2018 One more thing: since that issue has been discussed we have $page->references();: https://processwire.com/blog/posts/processwire-3.0.107-core-updates/#page-gt-references Maybe it is time to rethink it? 1 Link to comment Share on other sites More sharing options...
kongondo Posted September 28, 2018 Share Posted September 28, 2018 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 More sharing options...
szabesz Posted September 28, 2018 Share Posted September 28, 2018 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 More sharing options...
Robin S Posted September 28, 2018 Share Posted September 28, 2018 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... 2 Link to comment Share on other sites More sharing options...
kongondo Posted September 28, 2018 Share Posted September 28, 2018 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now