Extending Text Form Core component Model class to add a new field | Community
Skip to main content
Level 9
March 12, 2025
Solved

Extending Text Form Core component Model class to add a new field

  • March 12, 2025
  • 4 replies
  • 1151 views

Hi ,

 

Does anyone know how i could best extend core component model class for textinput forms using delegate pattern. Unable to get to it.I also do not want to complete the methods that come in from the ootb texinput class , but retain the code in the ootb class without the need for overriding methods.

 

Below code forces me to override methods and does not get the showcharcounter custom method

 

@ConsumerType

public interface AFTextInputModel extends TextInput{

boolean showCharCounter();

}

 

 

@Model(

adaptables = { SlingHttpServletRequest.class},

adapters = { AFTextInputModel.class, ComponentExporter.class },

resourceType = AFTextInputModelImpl.RESOURCE_TYPE)

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

public class AFTextInputModelImpl implements AFTextInputModel{

 

@Self

@2434638(type = ResourceSuperType.class)

private TextInput tinput;

 

@ValueMapValue

protected boolean showCharCounter;

 

 

public boolean showCharCounter() {

logger.info("char counter :: {}",showCharCounter);

return showCharCounter;

}

}

Best answer by arunpatidar

Thanks @arunpatidar 

1. do you know if I should stick with custom impl or textinput OOTB in htl class ? I see different answers around and both work 

 

2. Skipping res types and looks ok , I concur thanks for seconding that . Another point , do I need to extend abstract component impl or the abstract base impl . I wouldn’t think so since textinput would inherit it  

 

3. You mean the junit throwing NPE for textinput OOTB object(through resource super type) from the custom class is normal ? Yes in the custom Java impl class the object was not null . Coverage goes up much less due to this and need to override methods that are mandatory that I don’t touch unfortunately 

 

4. I tried adding embed to the custom textinput client library  with the category the OOTB email telephone component client library has but the embed did not load the script for the email and telephone core form components. 

5. Granite.i18n.get() I get an exception - undefined on get so I tried the granite.utils as dependency to the custom textinput client library but it didn’t load . Any ideas ?


1. do you know if I should stick with custom impl or textinput OOTB in htl class ? I see different answers around and both work  - If you use your own custom interface/imp in HTL class that means you need to overide HTL from core to custom project and need maintainability. 

 

2. Skipping res types and looks ok , I concur thanks for seconding that . Another point , do I need to extend abstract component impl or the abstract base impl . I wouldn’t think so since textinput would inherit it  -   Not needed. 

 

3. You mean the junit throwing NPE for textinput OOTB object(through resource super type) from the custom class is normal ? Yes in the custom Java impl class the object was not null . Coverage goes up much less due to this and need to override methods that are mandatory that I don’t touch unfortunately - Yes, in your Test class if you use OOTB interface Textinput. 

 

4. I tried adding embed to the custom textinput client library  with the category the OOTB email telephone component client library has but the embed did not load the script for the email and telephone core form components. - OK then try override clientlibs as well

5. Granite.i18n.get() I get an exception - undefined on get so I tried the granite.utils as dependency to the custom textinput client library but it didn’t load . Any ideas ? - Granite is available OOTB in author mode, not sure if you need this on Publisher.

4 replies

DPrakashRaj
Community Advisor
Community Advisor
March 12, 2025

Can you check inside /apps folder this component cq/experience-fragments/editor/components/experiencefragment has any html or jsp file? It might be possible that this HTML or jsp file is getting called instead of your custom component HTML 

Level 9
March 12, 2025

Hi i am not using experience fragment. So i use the custom model created in the HTL and i now get the value but i do not want to override the ootb textinput class methods. How do i avoid it?

DPrakashRaj
Community Advisor
Community Advisor
March 13, 2025
arunpatidar
Community Advisor
Community Advisor
March 14, 2025

Hi @nitrohazedev 

Please find example here

https://aemsimplifiedbynikhil.wordpress.com/2023/02/27/extending-core-components-model-and-creating-its-new-implementation-aem/ 

 

you don't need this part

@ConsumerType public interface AFTextInputModel extends TextInput{ boolean showCharCounter(); }

 

you can directly implement TextInput with your impl

public class AFTextInputModelImpl implements TextInput{

 

 

Arun Patidar
Level 9
March 14, 2025

Hi @arunpatidar  , yes I followed it in my change yesterday .. thank you. 
1. The htl should be pointing to text input or the custom class , I am inclined to go with custom class AFTextInputModelImpl but a blog stated to use TextInput . Surprisingly both seemed to work when implemented 

 

2. another question @arunpatidar , the textinput OOTB has in the model annotation resource type for telephone email and textinput field so I have that as well in custom class although no changes for email telephone but while I have OOTB telephone email the custom textinput is used. Do I need to add the resource types for email and telephone ? 

 



3. I didn want to override methods but using delegate(Lombok) I am forced to implement few other abstract methods (not all) so I do a textinput.get.. in the getters . However unable to instantiate the superrestype textinput in test class and hence is null .

@Self

@Via(type = ResourceSuperType.class)

@Delegate

private TextInput tinput;

 

@Override

public Date getExclusiveMaximumDate() {

return null != tinput ? tinput.getExclusiveMaximumDate() : null;

}

Test class:

assertEquals(null, afTextInputModelImpl.getFieldType());//throws null for tinput and hence workaround in getter above

 

4. Client libs , I see the telephone email and textinput have category “xyz” in themOOTB ,  and I want to alter only the js for textinput .. when I create a custom client lib “customxyz” and retain the original (to have email telephone to work) , I see both the OOTB textinput js and custom textinput js loading causing conflicts/race condition . I have an alternative to bring in the client libs for email and telephone and rename to “customxyz” , but I have no changes to email telephone . What’s the best way to approach ?



 

arunpatidar
Community Advisor
Community Advisor
March 14, 2025

2. Not needed if you don't want to execute your implementation for those types(email and telephone)

3. I have seen this problem only for Junit Test class but for normal class supertype was not null. Example

4. yes, thats true, the clientlibs are loaded based on category, so all the clientlibs with same category will be loading.Instead of copying clienlibs if possible you can load telephone and email clientlibs using embed/dependency property.

 

Arun Patidar
Level 9
March 20, 2025

All, working with Adobe will post updates soon 

kautuk_sahni
Community Manager
Community Manager
June 23, 2025

@nitrohazedev if you discovered a solution on your own, we'd appreciate it if you could share it with the community. Thank you.

Kautuk Sahni