Unable to Inject htl parameters on publish instance | Community
Skip to main content
Level 3
May 13, 2022
Solved

Unable to Inject htl parameters on publish instance

  • May 13, 2022
  • 3 replies
  • 1413 views

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 { @586265 private String productId; @586265 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>

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Hi,

Please check this if helps

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ParamModel.java 

3 replies

Level 4
May 14, 2022

could you please try @valuemapValue instead of @586265 annotation 

DNest19Author
Level 3
May 16, 2022

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

SantoshSai
Community Advisor
Community Advisor
May 14, 2022

Dear @dnest19  

Please check Sling Models documentation Apache Sling :: Sling Models

and for implementation 
1. http://sgaem.blogspot.com/2017/08/deep-dive-on-sling-models-part-1.html
2. https://www.techinnovia.com/sling-model/

Hope that helps you!
Regards,
Santosh


Santosh Sai
arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
May 15, 2022
DNest19Author
Level 3
May 16, 2022

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);
    }
}

 

DNest19Author
Level 3
May 16, 2022

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;