Jump to content

Image deleting from API not working?


soyerH
 Share

Recommended Posts

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

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

image.thumb.png.ff724bb287997bb65c462f0a717ae918.png

And here's an example to delete the first image ("all images" in the case of a single image field)

Spoiler

image.png.3a5a5d244edc0284b74becc31aa8541f.png

 

$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');



 

  • Thanks 1
Link to comment
Share on other sites

Posted (edited)

Hello Sanyaissues

Thank you, but still no luck. Have tested both as single image and multiple images.

Here is Tracy log

Spoiler

1402918633_photo_2024-08-08_14-57-13(2).thumb.jpg.ca9b06e0221915130d7b00faa2bec979.jpg

Spoiler

photo_2024-08-08_14-57-13.thumb.jpg.e0b8932c7d03ed3e85a9ed487c10293f.jpg

Will much appreciate further help ? 

Edited by soyerH
forget to hide images under spoiler
Link to comment
Share on other sites

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

image.thumb.png.ff724bb287997bb65c462f0a717ae918.png

And here's an example to delete the first image ("all images" in the case of a single image field)

  Reveal hidden contents

image.png.3a5a5d244edc0284b74becc31aa8541f.png

 

$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:

spacer.png

Doesn't work on all other pages:

spacer.png

 

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

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 by da²
Link to comment
Share on other sites

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

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

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

@soyerH, calling deleteAll() on a single image field is also working for me, so something unusual is going on for you.

image.png.9298b41f2e9c78cb0563a4aa1a6360ed.png

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()?

image.png.c057bf741f106bb4c6f37a30be2ab52b.png

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