Highlighted

Issue with Navigation component using Context Aware Configuration in Editable Templates

Avatar

Avatar

kiranv40650953

Avatar

kiranv40650953

kiranv40650953

01-02-2019

Hi,

I have implemented dynamic Navigation component using Context Aware Configuration in Editable Templates.

I have followed Jörg Hoh  blog Sling Context-Aware configuration (part 2) | Things on a content management system

I have  Site1/en and Site2/fr

en have subpages like demo1,demo2...etc which are in navigation as well.

fr have subpages like test1,test2...etc which are in navigation as well.

Default path which i gave NavigationRootCAConfig is /content/site1/en.

Here I'm facing issue.Suppose if i'm in Site2/fr page i should get test1 and test2 in navigation but i'm getting demo1 and demo2.

It is always taking default path if i specify at structure level if i add login in any component and drag & drop i'm getting latest path.

Navigation component not taking latest page path in Editable template. I can see latest path in my in my logs.

Here is my code

1.NavigationRootCAConfig.java

@Configuration(label = "Navigation Root Context Aware Config")

public @interface NavigationRootCAConfig {

   @Property(label = "Navigation Root")

  String navigationRoot() default "/content/site1/en";

}

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class LeftNav {

   protected final Logger logger = LoggerFactory.getLogger(LeftNav.class);

   private String DEFAULT_NAV_ROOT = "/content/site1/en";

   private List<Page> navItems = new ArrayList<>();

   private Page currentPage;

   private String navigationRoot;

   @Inject
  @Optional
   private boolean collectAllPages;

   @Inject
  @Optional
   private int structureDepth;

   @Self
  @Via("resource")

  Resource resource;

   @SlingObject
   ResourceResolver resourceResolver;

   @Inject
   ComponentService componentService;

   @PostConstruct
   protected void init() {

   logger.info("Nav Root Path : " + navigationRoot);

   logger.info("Collect All Pages : " + collectAllPages);

   logger.info("Structure Depth : " + structureDepth);

   if (StringUtils.isBlank(navigationRoot)) {

   navigationRoot = getNavigationRoot();

   logger.info("Navigation root {} .....", navigationRoot);

   navItems.addAll(componentService.getNavItems(navigationRoot, collectAllPages, structureDepth));

  }

  }

   public List<Page> getNavItems() {

   return navItems;

  }

   public String getNavigationRoot() {

   currentPage = resourceResolver.adaptTo(PageManager.class).getContainingPage(resource);

  String path = currentPage != null ? currentPage.getPath() : "";

   if (!StringUtils.isBlank(path) && resource != null) {

   NavigationRootCAConfig navRootCAConfig = resource.adaptTo(ConfigurationBuilder.class).as(NavigationRootCAConfig.class);

   logger.info("Navigation root path {} *** ", navRootCAConfig.navigationRoot());

   navigationRoot = navRootCAConfig.navigationRoot();

  } else {

   navigationRoot = DEFAULT_NAV_ROOT;

  }

   return navigationRoot;

  }

   public void setNavigationRoot(String navigationRoot) {

   this.navigationRoot = navigationRoot;

  }

   public boolean isCollectAllPages() {

   return collectAllPages;

  }

   public void setCollectAllPages(boolean collectAllPages) {

   this.collectAllPages = collectAllPages;

  }

   public int getStructureDepth() {

   return structureDepth;

  }

   public void setStructureDepth(int structureDepth) {

   this.structureDepth = structureDepth;

  }

   public Page getCurrentPage() {

   return componentService.getCurrentPage(navigationRoot, collectAllPages, structureDepth);

  }

   public void setCurrentPage(Page currentPage) {

   this.currentPage = currentPage;

  }

   public void setNavItems(List<Page> navItems) {

   this.navItems = navItems;

  }

Can anyone help here?

Thanks,

Kiran

Replies

Highlighted

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

891

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

891

Correct Answer

1.0K
Jörg_Hoh
Employee

01-02-2019

Hi,

can you provide your example in a github repo, so I can clone and analyze it? I find it hard to diagnose it here 🙂

How does your structure in /conf look like?

Thanks,

Jörg

Highlighted

Avatar

Avatar

rajeevy89244319

Avatar

rajeevy89244319

rajeevy89244319

13-04-2020

@Jörg_Hoh 

Does Context aware configuration works for assets, tags, XF as well or it just support site content?

Highlighted

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

891

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

891

Correct Answer

1.0K
Jörg_Hoh
Employee

14-04-2020

From an API point of view it is not limited to Sites/pages.