Expand my Community achievements bar.

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

Avatar

Level 10

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)

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

public class AFTextInputModelImpl implements AFTextInputModel{

 

@Self

@Via(type = ResourceSuperType.class)

private TextInput tinput;

 

@ValueMapValue

protected boolean showCharCounter;

 

 

public boolean showCharCounter() {

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

return showCharCounter;

}

}

12 Replies

Avatar

Community Advisor

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 

Avatar

Level 10

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?

Avatar

Community Advisor

Oh ok got it. Please check below link it will help you in tracing the issue https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/delegation-pattern-for-sli...

Avatar

Level 10

Thanks a lot. Tried but it asks for below to still be implemented via override seems like abstract methods. Delegate works for Title class not the specific form Textinput class for some reason else i get an error "The type AFTextInputModelImpl must implement the inherited abstract method DateConstraint.getExclusiveMaximumDate()"


Date getExclusiveMaximumDate();
Date getExclusiveMinimumDate();
Long getMaximum();
Long getMinimum();
Date getMaximumDate();
Date getMinimumDate();
Long getExclusiveMaximum();
Long getExclusiveMinimum();
Integer getMaxLength();
Integer getMinLength();
Integer getFieldType();

Avatar

Community Advisor

As explained on https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/delegation-pattern-for-sli... with delegate you have to either override it in your class and uses method call to super class in your override methods or get the instance of super class and use their method with your model class call in your html

Avatar

Level 10

Thanks @DPrakashRaj utilized it but the class still forces me to override few methods not all so used supertype.get in custom class getters

Avatar

Community Advisor

Hi @NitroHazeDev 

Please find example here

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

 

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

Avatar

Level 10

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 ? 

NitroHazeDev_0-1741959592683.png

 



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 ?



 

Avatar

Community Advisor

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

Avatar

Level 10

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 ?

Avatar

Community Advisor

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.



Arun Patidar

Avatar

Level 10

All, working with Adobe will post updates soon