Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Unable to Inject htl parameters on publish instance

Avatar

Level 2

I am converting WCMusePojos to sling models and have run across an issue that I have been unable to resolve. For some reason the parameters from my HTL isn't being injected on the publish instance. This works fine on the author instance, but both variables are null on publish when I check the logs. What could be causing this issue? 

 

Here's the model:

 

 

package com.my.aem.dam.core.portal;

import com.day.cq.wcm.api.components.ComponentContext;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

/**
 * Helps setting the component context with
 * the product id passed as parameter from the HTL file
 * so it can be used by other components on the page
 * */
@Model(
        adaptables = {SlingHttpServletRequest.class}
)
public class IdHelper {

    @inject
    private String productId;

    @inject
    ComponentContext context;

    private static final String PRODUCT_ID = "productId";
    private Logger logger = LoggerFactory.getLogger(IdHelper.class);

    /**
     * Obtains the product id as a parameter of the HTL and then sets it as
     * attribute of the component context.
     * */
    @PostConstruct
    public void activate() {
        logger.info("productId is {}", productId);
        logger.info("CompontentContext is {}", context);
        context.setAttribute(PRODUCT_ID, productId);
    }
}

 

 

And the HTL:

 


<div data-sly-use.accordion="com.adobe.cq.wcm.core.components.models.Accordion"
data-sly-use.modelCache="com.adobe.aem.commons.assetshare.util.ModelCache"
data-sly-test.asset="${modelCache['com.adobe.aem.commons.assetshare.content.AssetModel']}"
data-sly-use.productAccordion="${'com.my.aem.dam.core.portal.ProductAccordion' @ path=asset.path, addRepeatingNames=true}"
class="cmp-accordion"
data-cmp-is="accordion"
data-cmp-single-expansion="${accordion.singleExpansion}">
<div data-sly-test="${productAccordion.products.size > 0}"
data-sly-repeat.item="${productAccordion.products}"
class="cmp-accordion__item"
data-cmp-hook-accordion="item"
data-cmp-expanded="${item.name in productAccordion.products}">
<h3 data-sly-element="${item.name}"
class="cmp-accordion__header">
<button class="cmp-accordion__button${item.name in accordion.expandedItems ? ' cmp-accordion__button--expanded' : ''}"
data-cmp-hook-accordion="button">
<span class="cmp-accordion__title">${item.id}</span>
<span class="cmp-accordion__icon"></span>
</button>
</h3>
<div data-sly-use.productHelper="${'com.my.aem.dam.core.portal.IdHelper' @ productId=item.id}"
data-sly-resource="/content/my-portal/home/details/product-accordion-container/jcr:content/root/responsivegrid"
data-cmp-hook-accordion="panel"
class="cmp-accordion__panel${item.name in accordion.expandedItems ? ' cmp-accordion__panel--expanded' : ' cmp-accordion__panel--hidden'}"
role="region"></div>
</div>
<sly data-sly-resource="${resource.path @ resourceType='wcm/foundation/components/parsys/newpar', appendPath='/*', decorationTagName='div', cssClassName='new section aem-Grid-newComponent'}"
data-sly-test="${(wcmmode.edit || wcmmode.preview) && accordion.items.size < 1}"></sly>
</div>

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
6 Replies

Avatar

Level 2

could you please try @valuemapValue instead of @inject annotation 

Avatar

Level 2

I tried and they were still null on publish, but now also null on author.

Avatar

Correct answer by
Community Advisor

Avatar

Level 2

I was able to populate the portalId based on your suggestion, but the componentcontext is still null. Do you have any suggestions for that particular issue? Thank you for your help!

package com.my.aem.dam.core.portal;

import com.day.cq.wcm.api.components.ComponentContext;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

/**
 * Helps setting the component context with
 * the product id passed as parameter from the HTL file
 * so it can be used by other components on the page
 * */
@Model(
        adaptables = {SlingHttpServletRequest.class, Resource.class},
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class IdHelper {

    @RequestAttribute(name="productId")
    private String productId;

    @Inject
    ComponentContext context;

    private static final String PRODUCT_ID = "productId";
    private Logger logger = LoggerFactory.getLogger(IdHelper.class);

    /**
     * Obtains the product id as a parameter of the HTL and then sets it as
     * attribute of the component context.
     * */
    @PostConstruct
    public void activate() {
        logger.info("productId is {}", productId);
        logger.info("CompontentContext is {}", context);
        context.setAttribute(PRODUCT_ID, productId);
    }
}

 

Avatar

Level 2

After some tinkering I have managed to get it to work based on your resource. Thank you so much!

 

Here's how I injected the context: 

    @ScriptVariable(name="componentContext")
    ComponentContext context;