adobe / aem-core-wcm-components

Standardized components to build websites with AEM.
https://docs.adobe.com/content/help/en/experience-manager-core-components/using/introduction.html
Apache License 2.0
730 stars 736 forks source link

Create @ValueMapOrStyleValue and @StyleValue injection annotations and associated injectors. #855

Closed paul-bjorkstrand closed 4 years ago

paul-bjorkstrand commented 4 years ago

The code pattern

properties.get(PN_PROP_NAME, currentStyle.get(PN_PROP_NAME, PROP_DEFAULT_VALUE));

is used so frequently, and is repetitive boilerplate. I think an annotation-based injection could shortcut a decent chunk of the boilerplate, mostly in the @PostConstruct method, but also removing the need for @ScriptVariable ValueMap properties and @ScriptVariable Style currentStyle.

As an added bonus, it might give incentive to development teams to more often use the design dialogs, since it takes away the extra code needed to handle the defaulting to the Style.

Additionally, having a stand-alone @StyleValue would make using Style properties as easy to use as Value Map properties.

Potential annotation implementation:

public @interface ValueMapOrStyleValue {
  // Would work the same as @ValueMapValue name()
  public String name() default "";

  // Would default to name(), and then the field name, like name() does
  public String styleName default "";

  // Works the same as in @ValueMapValue, but only fails when both Value Map and Style do not have a value, when this is REQUIRED.
  public  InjectionStrategy injectionStrategy() default InjectionStrategy.DEFAULT;  
}

The annotation for @StyleValue would be nearly identical to @ValueMapValue

bpauli commented 4 years ago

@paul-bjorkstrand This kind of injector is available since AEM 6.4. Please have a look at [1] for more information. As the core-components are backward compatible to AEM 6.3 this has not been integrated yet. Once we drop the support for AEM 6.3 we can migrate to this new injector.

[1] https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/com/day/cq/wcm/models/annotations/injectorspecific/StyleOrValueMapValue.html