corcel / acf

Advanced Custom Fields (ACF) plugin for Corcel
127 stars 100 forks source link

Gallery field "undefined offset" error #51

Closed omzy closed 6 years ago

omzy commented 7 years ago

I was uploading some images as part of a gallery field. During the upload there was a server error and the image did not save correctly to the DB. However it did upload the image and create the thumbnails.

When I loaded the page in the browser, I got an error:

image

Here is the function where the error is occurring:

image

The problem is on line 40 we are assuming that $attachment->ID will always exist in $metaDataValues array, which in my case was not true. A simple array_key_exists() check is therefore necessary:

foreach ($attachments as $attachment) {
    if (array_key_exists($attachment->ID, $metaDataValues)) {
        $image = new Image($this->post);
        $image->fillFields($attachment);
        $image->fillMetadataFields($metaDataValues[$attachment->ID]);
        $this->images[] = $image;
    }
}

Please let me know if you will add this in or if you want me to send a PR.

omzy commented 6 years ago

What do you think @jgrossi?

jgrossi commented 6 years ago

@omzy83 sorry about being MIA and thanks for sharing! go ahead and send a PR ;-) thanks

omzy commented 6 years ago

@jgrossi PR is here: https://github.com/corcel/acf/pull/55