3

When I export data using Craft's built-in Element Exporter, asset fields are exported with an element ID. This is the same as Category and Entry fields, for example.

However, when mapping fields in Feed Me, asset fields do not allow you to select ID as the provided data, like a Category or Entry field would. When you run import, these fields seem to be skipped as the provided data is not what Feed Me is looking for.

Perhaps I'm missing something here, but Element Exporter and Feed Me don't seem compatible when it comes to asset fields at the moment. Has anyone else had any luck using these two programs together with regards to assets?

1

I don't know if you're still facing this issue but it's something I'm working on right now! Basically my solution was to create a short program to parse through the Element Exporter json and to change anything that was an Asset ID to that asset's url (which Feed Me accepts).

    public function modifyExport(array $element): array {
        $result = [];
        //Parse through JSON export
        foreach ($element as $key => $value) {
            //Recursive function to keep traversing down levels
            if (is_array($value)) {
                $result[$key] = $this->modifyExport($value); 
            //You've reached a root level, check the content
            } else {
                //If the value is a number, it is representing an ID
                if (is_numeric($value)) {
                    //Find the Element corresponding to this id
                    $assetCheck = Craft::$app->getElements()->getElementTypeById(intval($value));
                    //Check if the element is an asset
                    if ($assetCheck == 'craft\\elements\\Asset') {
                        //Get the actual asset data
                        $asset = Craft::$app->getElements()->getElementById(intval($value));
                        //Create the asset path *this may differ based on where your assets are*
                        $path = getEnv('DEFAULT_SITE_URL') . '/assets/images/' . $asset->folderPath . $asset->filename;
                        //Rewrite the content with the path, instead of the id
                        $result[$key] = $path;
                    //If the element is not an asset, keep it the same
                    } else {
                        $result[$key] = $value;
                    }
                     
                }
            }
        }
        return $result;
    }

Once I got the result, I used file_put_contents('end/path/goes/here.json', json_encode('result here')); to place it in a different file OR you can simply overwrite the same export file. Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.