Home > Mobile >  Magento 2 attribute image multiple
Magento 2 attribute image multiple

Time:12-28

I have an entity which has an EAV attribute image to display one image in the BO. This image is an image I get from an API call...nothing extraordinary... But I would like to modify it because the API will send several images. So I would like to have the possibility of saving all these images and to display all these images. What is the best way to do it? Do I have to add another attribute? ? But as I can receive 10 images max... how can I do it? In a first time I was think adding in my entity 9 field image2 image3 image4 etc...But I think there is a way more efficient to save and display my images.

Thanks in advance for your help

(I am beginner in magento 2)

Here is a part of the part of my controller saving the photos after getting it from the API

                case 'mainPhoto':
                if (
                    is_array($retailerData['mainPhoto']) &&
                    isset($retailerData['mainPhoto']['publicUrl'])
                ) {
                    $imageUrl = $retailerData['mainPhoto']['publicUrl'];
                    $fileName = baseName($imageUrl);
                    $newFileName = $this->getMediaSellerPath() . DIRECTORY_SEPARATOR . $fileName;
                    $result = $this->file->read($imageUrl, $newFileName);
                    if ($result) {
                        $model->setMediaPath($fileName);
                    }
                }
                break;
            case 'photos':
                $test = [];
                foreach ($value as $key => $item) {
                    if (
                        is_array($item) &&
                        isset($item['publicUrl'])
                    ) {
                        $imageUrl = $item['publicUrl'];
                        $fileName = baseName($imageUrl);
                        $newFileName = $this->getMediaSellerPath() . DIRECTORY_SEPARATOR . $fileName;
                        $test[] = $newFileName;
                        $result = $this->file->read($imageUrl, $newFileName);
                        if ($result) {
                            $model->setCustomAttribute(RetailerAttributeInterface::PHOTOS,  implode(",", $test));
                        }
                    }
                }
                break;
        }

Actually I can save in the database the photos separated by a comma but when i get in the module I have the error

Cannot gather stats! Warning!stat(): stat failed for /var/www/xxx/pub/media/seller/medium_lRmMjKLDWV.jpg,medium_vyhkzfEhho.jpg

How can I save the 2 images separated by a comma in a custom attribute without this problem

CodePudding user response:

First, you need to save the image to pub/media, then create an attribute with the type of text and save the entire image name in this field (separate each other with a ',') then when displaying you just call the data in this field and use the function to cut the data after the ',' and call pub/media to get the specific image path.

CodePudding user response:

It's ok I have changed my EAV attribute type from image to text and I do no have this error.

Thanks

  • Related