soyerH Posted August 7 Share Posted August 7 Hi all. Sorry for bad english. I have strange problem with deleting images from field trough API. Context: page with single file image field, uploading image trough UI. Than I go $page_id = 9000; $field_name = 'test_image_field'; $p = wire('pages')->get($page_id); $p->of(false); $p->$field_name->deleteAll(); $p->save($field_name); And... nothing happens. The image is still there. Maybe someone had same problem or experience with a bug or advice how to test the problem. Fought for two days with this. Really tired ? Thanks in advance! Link to comment Share on other sites More sharing options...
Sanyaissues Posted August 7 Share Posted August 7 Check this post from Ryan If test_image_field contains multiple images your code will work. As you can see in this test we deleted 3 items from the `image` field. Spoiler And here's an example to delete the first image ("all images" in the case of a single image field) Spoiler $page_id = 1035; $field_name = 'image'; $p = wire('pages')->get($page_id); $p->of(false); d($p->$field_name); $p->$field_name->delete($p->$field_name->first()); d($p->$field_name); $p->save($field_name); If the error persists please install Tracy, run this in the console and share me the ouput: $p = pages(9999); $p->of(false); d($p->test_image_field); $p->test_image_field->deleteAll(); d($p->test_image_field); $p->save('test_image_field'); 1 Link to comment Share on other sites More sharing options...
soyerH Posted August 8 Author Share Posted August 8 (edited) Hello Sanyaissues Thank you, but still no luck. Have tested both as single image and multiple images. Here is Tracy log Spoiler Spoiler Will much appreciate further help ? Edited August 8 by soyerH forget to hide images under spoiler Link to comment Share on other sites More sharing options...
QuerCoder Posted August 8 Share Posted August 8 15 hours ago, Sanyaissues said: Check this post from Ryan If test_image_field contains multiple images your code will work. As you can see in this test we deleted 3 items from the `image` field. Reveal hidden contents And here's an example to delete the first image ("all images" in the case of a single image field) Reveal hidden contents $page_id = 1035; $field_name = 'image'; $p = wire('pages')->get($page_id); $p->of(false); d($p->$field_name); $p->$field_name->delete($p->$field_name->first()); d($p->$field_name); $p->save($field_name); If the error persists please install Tracy, run this in the console and share me the ouput: $p = pages(9999); $p->of(false); d($p->test_image_field); $p->test_image_field->deleteAll(); d($p->test_image_field); $p->save('test_image_field'); Hi everyone, I'm writing on behalf of the original poster, and we're continuing to investigate the issue together. Thank you all for your suggestions and support so far! We've done some additional debugging and noticed something interesting. On the page where deleteAll works correctly, calling isChanged($field_name) returns true, as expected. However, on all other pages where deleteAll fails, isChanged($field_name) returns false. I've attached two screenshots showing the debug results: one from the page that works and one from a page that doesn't. Spoiler Works only on one page: Doesn't work on all other pages: Looking at the remove function in \wire\core\WireArray.php, it seems like the issue might be related to trackChange or trackRemove not being triggered for some reason. Could this be the root of the problem? Any insights or ideas on what could be causing this would be greatly appreciated. Thanks again for your help! Link to comment Share on other sites More sharing options...
da² Posted August 8 Share Posted August 8 (edited) Reading the Ryan message, the key is to load the field unformatted. $field = $page->getUnformatted('myImageField'); $field->deleteAll(); $page->save(); Be sure to dot that before to investigate deeper. ? Edited August 8 by da² Link to comment Share on other sites More sharing options...
QuerCoder Posted August 8 Share Posted August 8 2 minutes ago, da² said: Reading the Ryan message, the key is to load the field unformatted. $field = $page->getUnformatted('myImageField'); $field->deleteAll(); $page->save(); Be sure to dot that before to investigate deeper. ? We've certainly tried it, and it doesn't work for us. Link to comment Share on other sites More sharing options...
QuerCoder Posted August 8 Share Posted August 8 We've managed to solve the problem by adding: $p->trackChange($field_name); right before: $p->deleteAll($field_name); This solution works for us, but we're still curious — why did this issue occur in the first place? Why doesn't deleteAll work without manually triggering trackChange? Any insights would be appreciated! Thanks again for the help! Link to comment Share on other sites More sharing options...
da² Posted August 8 Share Posted August 8 (edited) Yes, looks like there are a few bugs in change detection, I reported a similar one recently: https://github.com/processwire/processwire-issues/issues/1947 You could also try setAndSave('property', value) or save('property'), or reassigning the field to the page... Edited August 8 by da² Link to comment Share on other sites More sharing options...
Sanyaissues Posted August 8 Share Posted August 8 I tried a few more tests, and in my case, deleteAll() works every single time. But it seems like is a good idea to enable trackChange when modifying images. There are a few other examples in the forum, but they seem based on Soma's post. Hopefully someone else can explain us the why. Link to comment Share on other sites More sharing options...
Robin S Posted August 8 Share Posted August 8 @soyerH, calling deleteAll() on a single image field is also working for me, so something unusual is going on for you. A couple of ideas... 1. Is the page a "normal" page or is it a User page that involves the alternate template/parent for users feature? Your page path and field name made me wonder this. If it is an alternate user parent/template and you can identify the steps to reproduce the problem please create a GitHub issue for it. 2. Does it work if you set the field value to null instead of calling deleteAll()? 1 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