Uploaded image for project: 'Magnolia'
  1. Magnolia
  2. MAGNOLIA-6868

TemplatingFunctions for fetching content in templating: Prevent flooding logs of non resolvable JCR items

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Neutral Neutral
    • 5.5.1
    • None
    • core, templating
    • None
    • Yes
    • Kromeriz 75
    • 3

      The cmsfn provides:

      cmsfn.contentByPath(path, workspace)]
      cmsfn.nodeByPath(path, workspace)]
      cmsfn.contentById(id, workspace)]
      cmsfn.nodeById(id, workspace)]
      

      Two problems (1. & 2.) when the target node does not exists because of:

      • path outdated/invalid
      • id outdated/invalid

      1. Floods the logs with the thrown exceptions of trying to fetch the content.
      The templater ha no way to prevent the throwing of the exceptions, even he knows it is absolute valid an item does not exists.
      There are methods in jcr session to actually know if it exists before requesting it:

      jcrSession.nodeExists("pathToCheck");
      jcrSession.itemExists("pathToCheck");
      jcrSession.propertyExists("pathToCheck");
      

      There is no jcr method for checking if an id exists -> needs to catch the ItemNotFoundException

      2. Too many unnecessary if checks needed:
      -It would be nice for the templater to be able to check if a path or an id exists within a workspace, BEFORE trying to fetch the content of it.- ?hasContent in an if statement might be used instead

      -I added the needed Methods to info.magnolia.templating.functions.TemplatingFunctions and info.magnolia.jcr.util.SessionUtil as a patch.-

      -Here an example of how easy the code becomes, and no unnescessary exception is logged:-

          [#-- By Path (will produce logs): From --]
          [#if eventPath?has_content]
              [#assign eventNode = cmsfn.contentByPath(eventPath, "events")]
              [#if !eventNode?has_content]
                  [#-- Reacting on non resolvable path --]
              [/#if]
          [/#if]
      
          [#-- By Path: To --]
          [#if cmsfn.contentExists(eventPath, "events")]
              [#assign eventNode = cmsfn.contentByPath(eventPath, "events")]
          [#else]
              [#-- How ever to react on non existing path. But no need to check eventNode?has_content to get into the logical 'else' --]
          [/#if]
          
          
          [#-- By Id (will produce logs): From --]
          [#if eventId?has_content]
              [#assign eventNode = cmsfn.contentById(eventId, "events")]
              [#if !eventNode?has_content]
                  [#-- Reacting on non resolvable path --]
              [/#if]
          [/#if]
          
          [#-- By if: To --]
          [#if cmsfn.idExists(eventId, "events")]
              [#assign eventNode = cmsfn.contentById(eventId, "events")]
          [#else]
              [#-- How ever to react on non existing id. But no need to check eventNode?has_content to get into the logical 'else' --]
          [/#if]
      

        Acceptance criteria

              rkovarik Roman Kovařík
              cringele Christian Ringele
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved:

                  Task DoD