Updating imagesets through script

Hello, I have created a new image set on my local environment following the instructions here: Configure Image Variants - Bloomreach Experience Manager (PaaS/Self-Hosted) - The Fast and Flexible Headless CMS and added small/medium/large image sizes. On my local environment this worked very well, all images were updated with the new sizes.

Now I have deployed these changes to our staging environment which contains other images than the local environment. I thought it would be enough to update the existing images by running the UpdateImageSets script(which was also generated automatically when creating the new imageset) through the updater editor but this doesn’t seem to change anything. Is there anything else I should do to make this work? Or should I update the script myself? I couldn’t find anything about this in the documentation.

That script should update images with new definitions in that image set, it won’t change the image set.

So an image A, is of type hippogallery:imageset. If you add a new variant to hippogallery:imageset then the script will update the image. But if you make a new imageset myproject:imageset, then the image A won’t have it’s type changed, so will be unaffected by the new imageset.

1 Like

Hmm, but then why did it update the existing images on my local environment? I have now added a new image in the staging environment, but it also doesn’t create the small/medium/large sizes for that one.

So would you say that if I want to update all images on our staging&production environment, I have to create a script myself to set their imageset type to our custom imageset?

Ohh, I see now that in the console, on the folder for the images, the hippostd:gallerytype is set to hippogallery:imageset. So I should probably set that one to our custom imageset type right?

That will work for creating new imagesets, but the existing ones will still be of the old type. You will have to make an updater to change the type of the old imagesets.

Okay thanks, I will try that!

For future reference and if anybody else bumps into the same issue, here is the script, just set your own imageset type in the parameters. After running this script, your can run the UpdateImageSets scripts and it will create the new sizes for you:

import org.onehippo.repository.update.BaseNodeUpdateVisitor

import javax.jcr.Node
import javax.jcr.RepositoryException

/**
 * Groovy script to update the primary type of all imagesets to a new primary type
 *
 * XPath query: content/gallery//element(*, hippogallery:imageset)
 *
 * Parameters: { "primaryType": "yournamespace:yourimageset" }
 */
class ConvertImageSetTypeUpdater extends BaseNodeUpdateVisitor {

    boolean doUpdate(Node node) {
        String newPrimaryType = this.parametersMap["primaryType"]
        String nodeName = node.getName()

        log.debug(String.format("Attempting to update primary type of node %s from %s to %s", nodeName, node.getPrimaryNodeType().getName(), newPrimaryType));
        try {
            if (node.getPrimaryNodeType().getName() != newPrimaryType) {
                node.setPrimaryType(newPrimaryType);
                log.debug(String.format("Primary type of node %s updated to %s", nodeName, newPrimaryType));
            } else {
                log.debug(String.format("Primary type already set to %s", newPrimaryType));
            }
        } catch (RepositoryException e) {
            log.error(String.format("Could not update primary type of node %s to %s", nodeName, newPrimaryType), e)
            node.getSession().refresh(false/*keepChanges*/)
        }
    }

    @Override
    boolean undoUpdate(final Node node) throws RepositoryException, UnsupportedOperationException {
        return false
    }
}

3 Likes