Ad Code

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.

Monday, October 9, 2017

Apache Sling :: Sling Query in AEM 6.3


Hello Everyone,
SlingQuery is a sling resource tree traversal tool inspired by the jQuery JavaScript API.

Sling Query is a bundle which is not a part of AEM Felix console till now, but after knowing the bright side of this particular bundle,I am surprised that why it is not the part of AEM.
But not to worry, we can do it by ourselves and take the advantages of it.

We can directly upload this bundle in the Felix console and add the dependency in pom.xml
<dependency>
      <groupId>org.apache.sling</groupId>
      <artifactId>org.apache.sling.query</artifactId>
      <version>3.0.0</version>
      <scope> provided</scope>
</dependency>

                                             Why Sling Query?

Sling Query saves us from a lot of code writing. It is a good to use this functionality.
It provides us results as an iterator.
  • useful operations to traverse the resource tree,
  • flexible filtering syntax,
  • lazy evaluation of the query result,
  • SlingQuery object is immutable (thread-safe),
  • fluent, friendly, jQuery-like API.
                                     When to use Sling Query?

Before using this concept, we need to understand the use cases where sling query will be best suited.
It is not an alternative of JCR Queries at all.It doesn’t indexes and for large set of tree traversal, it’s performance is so slow.
As a rule of thumb -
  • if you have a complex Java loop reading resource children or parents and processing them somehow, rewriting it to SlingQuery will be a good choice.
  • If you have a recursive method trying to get some resource ancestor, using SlingQuery will be a good choice.
  • On the other hand, if you have a large resource subtree and want to find all cq:Pages, using SlingQuery is a bad choice.
pros and cons.PNG
Fig- Diff between JCR and Sling Query

Methods list of Sling Query
There are a lot of methods that can be used efficiently to make the programming task much easier.
1).$(resource): Creates a sling query object using passed resource as an initial collection.
Resource resource = resourceResolver.getResource(“/content/we-retail”);
$(resource)

2).children() : fetch all the child resources of a resource.
$(resource).listChildren();

3) .children(String filter): To iterate the children of a resource of particular resource  type(here cq:Page) or any other filter condition.
$(resource).children(“cq:Page”);
$(resource).children(“cq:Page[jcr:title=test]”);

4) .closest(String selector): The closest applies on the ancestors of the resource with all the conditions.First it will check the parent of the resource ,than its parents and it will keep on going.
$(resource).closest(“cq:Page”)

5).eq(int index): Sling Query returns a iterator. If there is a need of getting the resource on the basis of index, this function is important.
$(resource).listChildren(“cq:Page”).eq(0);

6).filter(String selector): Filter given collection based on some selector.
final Calendar someTimeAgo = Calendar.getInstance();
someTimeAgo.add(Calendar.HOUR, -5);
// get children pages modified in the last 5 hours
SlingQuery query = $(resource).children("cq:Page").filter(new Predicate<Resource>() { @Override
public boolean accepts(Resource resource) {
return resource.adaptTo(Page.class).getLastModified().after(someTimeAgo);
}});

7) .has(String selector): If the collection has the particular resource based on the selector.
// If the resource fetch all its children having a property value as  foundation/components/redirect”
$(resource).children().has("foundation/components/redirect")

8).add(Resource...resource): To add resources in a slingQuery Iterator object.
// Now the SlingQuery iterator will have all the children of a resource and resource itself.
$(resource).children().add($(resource)

9).asList: The iterator of SlingQuery can be converted to a list and get a particular resource on the basis of index.
// Get the path of the first child resource of a particular resource
$(resource).children("cq:Page").asList().get(0).getPath();

10).find(): search through the matched elements’ child, grandchild, great-grandchild…any levels down.For each resource in collection return all its descendants using selected strategy. Please notice that invoking this method on a resource being a root of a large subtree may and will cause performance problems.
//the resource finds all the richtext resources which can be find in the children,sub-children of the resource
$(resource).find(foundation/components/richtext);

11).next():
// If a resource is having child1, child2 and child3,child2 will be returned.
$(resource).children().first().next();

12).nextAll():
// If a resource is having child1, child2, child3 and child4, child2, child3 and child4  will be returned.
$(resource).children().first().nextAll();

13).nextUntil():Return all following siblings for each resource in the collection up to, but not including, resource matched by a selector.

// let's assume that resource have 4 children: child1, child2, child3 and child4 additionaly, child4 has property jcr:title=Page
// return child2 and child3
$(resource).children().first().nextUntil("[jcr:title=Page]");

14).parent: Return the parent of the resource.
$(resource).parent();

15).parents:For each element in the collection find all of its ancestors, optionally filtering them by a selector.
//create page breadcrumbs for the given resource
$(resource).parents(“cq:Page”);

16) .parentsUntil():For each element in the collection find all of its ancestors until a resource matching the selector is found.
// It will not include that resource which satisfies the condition of cq:page.
$(resource).parentsUntil(“cq:Page”);

17).prev():Return the previous sibling for each resource in the collection and optionally filter it by a selector. If the selector is given, but the sibling doesn't match it, empty collection will be returned.
// let's assume that resource have 3 children: child1, child2 and child3
// return child2
$(resource).children().last().prev();

18).prevAll():Return all preceding siblings for each resource in the collection, optionally filtering them by a selector.
// let's assume that resource have 3 children: child1, child2 and child3
$(resource).children().last().prevAll(); // return child1 and child2

19).prevUntil():Return all preceding siblings for each resource in the collection up to, but not including, resource matched by a selector. 
// let's assume that resource have 4 children: child1, child2, child3 and child4
// additionally, child1 has property jcr:title=Page
$(resource).children().last().prevUntil("[jcr:title=Page]"); // return child2 and child3

20).siblings():Return siblings for the given resources, optionally filtered by a selector.
$(resource).closest("cq:Page").siblings("cq:Page"); // return all sibling pages
$(resource).children("cq:Page").first().siblings();// return all sibling of first child page

21).slice():Reduce the collection to a sub-collection specified by a given range. Both from and to are inclusive and 0-based indices. If the to parameter is not specified, the whole sub-collection starting with from will be returned.
// let's assume that resource have 4 children: child1, child2, child3 and child4
$(resource).children().slice(1, 2); // return child1 and child2

22).searchStrategy():Select new search strategy, which will be used in following find() and has() function invocations. There 3 options:
DFS and BFS iterate through descendants using appropriate algorithm. QUERY strategy tries to transform SlingQuery selector into a SQL2 query and invokes it. Because there are SlingQuery operations that can't be translated (eg. :has() modifier), the SQL2 query result is treated as an initial collection that needs further processing. 
  1. $(resource).searchStrategy(SearchStrategy.BFS).find("cq:Page");
  2. $(resource).searchStrategy(SearchStrategy.DFS).find("cq:Page");
  3. $(resource).searchStrategy(SearchStrategy.QUERY).find("cq:Page");

23).first():Filter resource collection to the first element. Equivalent to .eq(0) or .slice(0, 0).
$(resource).siblings().first(); // get the first sibling of the current resource

24).last():Filter resource collection to the last element.
$(resource).siblings().last(); // get the last sibling of the current resource

25).not():Reduce the set of matched elements to those which doesn't match the selector. The selector may contain other modifiers as well, however in this case the function will be evaluated eagerly:
  1. $(resource).children().not("cq:Page"); // remove all cq:Pages from the collection
  2. $(resource).children().not(":first").not(":last"); // remove the first and the last element of the collection

Sling Query Modifiers
Sling Query object is immutable.
$(resource).listChildren().first();

Here three objects gets created ,because slingQuery object is immutable.
  • $(resource)
  • $(resource).listChildren()
  • $(resource).listChildren().first();
Instead of creating many objects, modifiers helps to create less number of objects.
  1. :eq(index) : resource at a particular index can be fetch using eq(index) modifier.
$(resource).find("weretail/components/content/title:eq(0)")

  1. :gt(index) : fetch all the resources greater than the index using gt(index)modifier.
$(resource).children("cq:Page:gt(2)");

  1. :lt(index): fetch all the resources less than the index value using lt(index) modifier.
$(resource).find("weretail/components/content/title:lt(8):first");

  1. :even : fetch all the resources at the even place of the iterator.
$(resource).find("foundation/components/richtext:even");

  1. :odd :fetch all the resources at the odd place of the iterator.
$(resource).find("foundation/components/richtext:odd");

  1. :first :fetch first resources of the iterator.
$(resource).children("cq:Page:first");

  1. :last :fetch lastresources of the iterator.
$(resource).children("cq:Page:last");

  1. :parent : fetch the parent of the resource.
$(resource).find("foundation/components/richtext:parent");

  1. :has(selector) :Reduce the set of the matched elements to those which have descendant matching the selector.
// get children pages containing richtext component
$(resource).children("cq:Page:has(foundation/components/richtext)]");

  1. :not(selector) :Reduce the set of matched elements to those which doesn't match the selector. The selector may contain other modifiers as well, however in this case the function will be evaluated eagerly:
$(resource).children("cq:Page:not(:last)");

Operator list

  • Contains [name*=value]
  • Contains a word [name~=value]
  • Has attribute [name]
  • Starts with [name^=value]
  • Ends with [name$=value]
  • Equals [name=value]
  • Not equal [name!=value]

Hierarchy operator list
  • Child operator (parent > child)
  • Descendant operator (ancestor descendant)
  • Next adjacent operator (prev + next)
  • Next siblings operator (prev ~ next)
References: https://github.com/Cognifide/Sling-Query/wiki/Hierarchy-operator-list

References:

Demonstration Video On Sling Query:


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.