Expand my Community achievements bar.

Remove component from Page model json

Avatar

Level 3

I have a requirement to remove the Column component and all its child component JSON from page model JSON if it contains a specific property set as "TRUE". 

 

I am trying to check for this property in Column Component Sling Model Exporter and setting the getExportedItems as null. Still, it does not remove the component and its child from the page model JSON.

 

package com.colorful.core.models;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ContainerExporter;
import com.adobe.cq.export.json.ExporterConstants;
import lombok.Getter;
import lombok.NonNull;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import java.util.Collections;
import java.util.Map;

import static com.colorful.core.models.ColumnComponentExporter.RESOURCE_TYPE;


@Model(adaptables = SlingHttpServletRequest.class,
        resourceType = RESOURCE_TYPE,
        adapters = {ColumnComponentExporter.class, ContainerExporter.class},
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class ColumnComponentExporter implements ContainerExporter {

    protected static final String RESOURCE_TYPE = "colorful/components/column";


    @ValueMapValue(name = "exportThisComponent")
    
    private boolean exportThisComponent;


    @NonNull
    
    public String getExportedType() {
        return RESOURCE_TYPE;
    }

    
    public Map<String, ? extends ComponentExporter> getExportedItems() {
        if (!exportThisComponent) {
            return Collections.emptyMap();
        }
        return null;
    }

    
    @NonNull
    public String[] getExportedItemsOrder() {
        return new String[0];
    }
}

 

 

How can I remove this column component and its child from page model JSON?

@DEBAL_DAS @arunpatidar @SantoshSai 

 

5 Replies

Avatar

Level 3

Using @JSONIgnore I can only remove specific properties from JSON.

But here I am trying to remove the entire "container" component object and its child from .model.json of the page.

ashish_mishra1_0-1679326714113.png

 

@arunpatidar 

 

Avatar

Community Advisor

Then remove

@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)

Avatar

Level 3

But I need to remove this column component from the page model json conditionally. 

http://localhost:4502/content/colorful/us/en.model.json

 

Like, if any component on en page has a property "hide": "true" then skip that component in the page model JSON.

How can I do that?

 

Avatar

Community Advisor

Hi,

I am not sure if Sling Filter and Sling rewrite pipeline can help here?