Ad Code

Showing posts with label @AemObject. Show all posts
Showing posts with label @AemObject. Show all posts

Wednesday, October 25, 2017

Useful Properties of Page Properties Dialog in AEM 6.3


Hello Everyone,

Generally we used to copy and paste the dialog and doesn’t understand the meaning of these attributes.
Here I am going to explain some of the page properties of dialog which are very useful.
So I am going to walk you through with each and every property in a detailed manner.
1.cq:showOnCreate :
  • As classic UI is soon going to be deprecated from AEM, and we have started using touch UI for page Creation.
  • When author create a page from sites, this needs to be notice that only two tabs are visible in the page properties dialog here.
tabs.PNG
Fig - Page Properties Dialog at the time page creation
Now the question comes that why only two tabs visible here while in the page properties dialog there are many tabs.The tabs/widgets are not visible in the dialog right now ,because if you see in the page properties dialog these tabs are having the property cq:showOnCreate as false.

So this property cq:showOnCreate plays a very important roles to show tabs or widgets at the time of page creation. That’s why if you want to author some fields at the time of page creation only, Just add this property with the value true in the tab /widget node.The default value of cq:showOnCreate is true.
advances.PNG
Fig - showOnCreate property added in tab node of the dialog
2.cq:hideOnEdit: While editing the page, if you want to hide any property from the page properties dialog ,set this value of cq:hideOnEdit as true.
The default value of cq:hideOnEdit is false.
hide.PNG
Fig - hideOnEdit property added in widget node of the dialog
Note: If you want to show any widget/tab at the time of page creation and at the time of edit you want to hide it, you can use a combination of cq:showOnCreate(as true) and cq:hideOnEdit(true).
3.allowBulkEdit : Sometimes many pages want to share same value of a widget(field).So there is no need to go to each and every page individually and change the property. Let suppose author want some tags to be shared across multiple pages. You can select more than one pages from the sites console and edit the property at all pages. This property enables the fields for bulk editing.

You can only bulk edit the pages:
  • Share the same resourceType
  • Are on part of live copy
bulk.PNG
Fig - allowBulkEdit property added in widget node of the dialog
4.cq-msm-lockable: One of the features built into AEM MSM is the ability to determine which properties of a page are inherited (“rolled out” in AEM terms) from the master site to its child sites (live copies).


msm locable.PNG
Fig - cq-msm-lockable property added in widget node of the dialog
msm.PNG
Fig - cq:LiveConfig node added under the live copy

How this property works:
1. This property will create the chain link in the dialog which indicates that these values will be fetched from the master copy.
Note: If the value is not present in the master copy, it will pick its own value.
2. This can be edited if inheritance from the master copy has been cancelled.

When cq-msm-lockable has been defined, breaking/closing the chain will interact with MSM in the following way:
The value of cq-msm-lockable can be relative and absolute.
a)Relative (e.g. myProperty or ./myProperty)
  • It will add and remove the property from cq:propertyInheritanceCancelled.
  • MSM does not operate with deep properties (e.g. ./image/fileReference), even though the dialog’s logic does. If the chain is opened a rollout of the page will overwrite ./image/fileReference, as the rollout of the image node will not "walk" up to the parent node to check cq:propertyInheritanceCancelled.

relative.jpg
Fig - Properties in case of relative path when inheritance cancelled

Note: H2Solutions provide a Hotfix for AEM MSM Inheritance breaking with deep     properties problem in the GitHub Repository

b)Absolute (e.g. /myProperty)
  • Breaking the chain will cancel inheritance by adding the cq:LiveSyncCancelled mixin to ./myProperty and setting cq:isCancelledForChildren to true.
  • Closing the chain will revert inheritance.
absolute.jpg
Fig - Properties in case of absolute path when inheritance cancelled

NOTE : When you re-enable inheritance, the live copy page property is not automatically synchronized with the source property. You can manually request a synchronization (rollout configuration) if this is required.
Demonstration Video On Properties of Page Properties Dialog:


If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com

Hope it will help you guys !!
Thanks and Happy Learning.

Sunday, August 20, 2017

Deep Dive on Sling Model in AEM 6.3 : Part-2


In our previous blog, we have already learned basics of sling models.But the magic of sling models is not over. We have more to know.
We have seen injector specific annotations of sling models in Part-1, except all these specific sling model Injectors we have some more annotations that help in Sling Models Development.
@Inject: This annotation is used to inject a property, resource, request anything.This is a generic annotation, which traverses all the sling model injectors based on service ranking.

@Model(adaptables = Resource.class)
public classTest {

  @Inject
  String path;

  public String getPath() {
      return path;
  }
}

@Named: If there is a need to change the getter of any attribute like (sling:resourceType, jcr:primaryType) @Named annotation helps to achieve this.

@Model(adaptables = Resource.class)
public class Test {

  @Inject @Named("sling:resourceType")
  String slingResourceType;

  public String getSlingResourceType() {
      return slingResourceType;
  }
}

@Default: A default value can be provided for Strings or primitive data types.If there is no value of that property, default value takes place.

@Model(adaptables = Resource.class)
public class Test {

  @Inject @Default(values = "/content/test")
  String path;

  public String getPath() {
      return path;
  }
}

@Optional and @Required: In the sling models, by default all the fields supposed to be required.Sometimes there is a need to mark them as optional and required specifically.So injector fields can be annotated with @Optional and @Required.

If a majority of @Injected fields/methods are optional, it is possible to change the default injection strategy by using adding defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL to the @Model annotation:

@Model(adaptables = Resource.class)
public class Test {

  @Inject @Optional
  String path;

  @Inject @Required
  String title;

  public String getTitle() {
      return title;
  }

  public String getPath() {
      return path;
  }
}
@Source: If you are using @Inject annotation and you want to specifically tell the sling engine that which specific injector you want to inject, you can use this annotation.All the specific injectors can also be used with @Source annotations.Using this annotation is equivalent to using injector specific annotation in a different way.

@Model(adaptables = SlingHttpServletRequest.class)
public class Test {

  @Inject  @Source("script-bindings")
  Page currentPage;

  public String getPath() {
      return currentPage.getPath();
  }
}
@Via : SlingHttpServletRequest has more objects than resource. Sometimes there is a need of using two injectors one from request and one from resource, Then we need to tell annotation explicitly that you are coming via resource.

@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel {
   
   // Injects ResourcePath with Via annotation
   @ResourcePath(path = "/etc/social") @Via("resource")
   Resource resource;
   
   // Injects currentPage from ScriptVariable adaptable to Request
   @ScriptVariable
   Page currentPage;
}
@PostConstruct: The @PostConstruct annotation can be used to add methods which are invoked upon completion of all injections: This method automatically gets called when a sling model instance is created.
Note:The name of the method doesn’t matter, only it is matters that on which method the PostConstruct annotation exist.

@Model(adaptables = SlingHttpServletRequest.class)
public class Test {

 @Inject @Source("script-bindings")
 Page currentPage;
 
  String path;

 @PostConstruct
 protected void postMethod() {
     path = currentPage.getPath();
 }

 public String getPath() {
     return path;
 }
}
The Demonstration video on @Inject, @Named, @Default, @Optional, @Required, @Source, @Via and @PostConstruct:


@AemObject Annotation by ACS Commons Package

ACS Commons package provides one more injector specific annotation named @AemObject.This annotation provides the support of a lot of objects shown below:
aemObject.PNG
Fig- List of object in @AemObject annotation provided by ACS Common
This annotation is not the part of sling models, so if sling models itself has all your needed injectors, no need to go for it.But some Objects are not available with sling models like Tagmanager, WorkflowSession, WCMMode, at that time, this annotation can help you. Remember your project must have dependencies of ACS-Commons before using this annotation.

@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel {

// Injects currentPage using ScriptVariable annotation
@AemObject
Page currentPage;

public String getPagePath() {
 currentPage.getPath();
}
}

List Injection From Child Resource(Since Sling Models Impl 1.0.6)
+- resource (being adapted)
|
+- content
   |
   +- subchild1
   |
   +- subchild

@Model(adaptables = Resource.class)
public classTest {

  @Inject
  List<Resource> content;
  
 public int getSize()
  {
      return content.size();
  }
}
Sling Adapter Framework
1.adaptTo: Apache Sling provides a way to adapt Sling related classes to our domain classes. The Resource and ResourceResolver interface provides the adaptTo method, which adapts the objects to other classes.
With the adaptTo API we can convert the Sling-related objects to our Model objects by using the AdapterFactory classes.
Test model = resource.adaptTo(Test .class)
As with other AdapterFactories, if the adaptation can't be made for any reason, adaptTo() returns null.
2.ModelFactory (Since1.2.0): Since Sling Models 1.2.0 there is another way of instantiating models. The OSGi service ModelFactory provides a method for instantiating a model that throws exceptions.There is no need of null checks and it is easier to see why sling model instantiation is failed. ModelFactory API provides a lot of methods, which can be efficiently used.

public class ModelServlet extends SlingSafeMethodsServlet {

@Reference
ModelFactory modelFactory;

@Override
protected void doGet(final SlingHttpServletRequest req,final SlingHttpServletResponse resp) throws ServletException, IOException {

 Resource resource = req.getResourceResolver().getResource("/content/community-components/en/tagcloud/jcr:content");

 Test test = modelFactory.createModel(resource, Test.class);
 resp.getWriter().println(test.getResourceType());
 resp.getWriter().println(modelFactory.canCreateFromAdaptable(resource, Test.class));
 resp.getWriter().println(modelFactory.canCreateFromAdaptable(req, Test.class));

}
}

@Model(adaptables = Resource.class)
public class Test {

 @Inject @Named("sling:resourceType")
  String resourceType;

  public String getResourceType()
  {
      return resourceType;
  }
}

The Demonstration video on @AemObject, List Injection and Adapter Framework:



If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com

Hope it will help you guys !!
Thanks and Happy Learning.