Jump to content

Multiple Image Bug (When using API) [RESOLVED]


Hani
 Share

Recommended Posts

I think I found a tiny bug when using the API and interacting with an image field.

I have an image field with the maximum number of files set to 1 in its configuration.  When you upload an image directly in the admin panel, it behaves properly and removes the existing image - replacing it with the new one.

However, whenever I add an image using the API, (example code below) it doesn't remove the existing image; it keeps both, ignoring the maximum number of files setting.

$page->image->add('http://www.domain.com/image.jpg');

Not a huge deal, but possibly something to put on the "to do" list.

I suppose I should ask if someone can confirm the bug and that it's not just me.

EDIT: This isn't a bug.  The behavior is expected.  Read Ryan's post for the reasoning & logic behind the behavior.

Link to comment
Share on other sites

Thanks guys.  I solved it by using $page->image->removeAll() which works just fine.

I'm not convinced that the behavior should be the way it is (even if it is intended).  If I upload an image in the admin, it hides the existing image using javascript immediately, but upon saving the page, the existing image is replaced with the new one.  I would think that the same behavior would apply to the API.

As mentioned before, if I upload multiple images with the API and go to edit the page, I'll see multiple photos in that field.  Essentially, this is an array of photos. The maximum file settings description says:

Enter 0 for no limit. If you enter 1, the API will reference this field as a single file rather than an array (when the Page's outputFormatting is on, as it is by default).

Maybe I'm on the wrong side of the fence here?  This isn't a huge issue and is easily solved by manually deleting the image using the API, but I figured it is worth discussing.

Edit: I do have to say that the current behavior can prevent accidental, and irreversible, deleting of assets.  So perhaps it is worth leaving it as is for that sole reason?

Edited by Hani
Link to comment
Share on other sites

When you are manipulating a page for the purpose of saving your changes via the API, then you want to make sure that the output formatting state is OFF. When output formatting is OFF then your images field will always behave as an array. The single-image reference (with setting 1) is primarily for front-end syntactic convenience so that you can do echo $page->image->url rather than $page->image->first()->url. But behind the scenes, it's still always an array. This reveals itself when you try to set a value to it and output formatting is on, for instance. But in general, it's not good to set any values you intend to save when output formatting is on. 

  • 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

×
×
  • Create New...