Get a piece of metadata off the current page

webadmin's Avatar

webadmin

17 May, 2016 06:25 PM

I've made a piece of custom metadata on a page that holds a date value. I'm currently getting the create date from the page, but I can't figure out how to get this metadata date. My plan is to check to see if the date is there. If it is, I'll use it, if not, I'll use the create date. How do I get it? I tried doing the trick of creating a format and looking at the current page, but that didn't seem to work.

This discussion was closed! See this FAQ for more information:

  1. Create a Calling Page Index Block

    This Index Block, which is usually referred to as a calling page or current page Block, is one of the most used Blocks in Cascade. To further explain the purpose behind it's functionality, this Block will access the stored content for whatever Page you are currently looking at and load it into what region this Block is assigned to.

    To create the Index Block:

    • Navigate to the BaseFolder → _cascade ...
    See more..
  1. webadmin closed this discussion on 17 May, 2016 06:27 PM.

  2. webadmin re-opened this discussion on 17 May, 2016 06:33 PM

  3. 1 Posted by webadmin on 17 May, 2016 06:33 PM

    webadmin's Avatar

    Actually, I'm still having problems. I can't seem to request the metadata that I added. I'm not sure how to ask for that. Here's the XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <system-index-block name="calling page" type="folder" current-time="1463509783049">
        <calling-page>
            <system-page id="627fa3740a01010200b255729d84e038" current="true">
                <name>title</name>
                <title>Title</title>
                <author>Author</author>
                <path>/blogs/news/2016/title</path>
                <site>Gatekeeper</site>
                <link>site://Gatekeeper/blogs/news/2016/title</link>
                <dynamic-metadata>
                    <name>Date</name>
                    <value>1-1-2008</value>
                </dynamic-metadata>
                <system-data-structure definition-path="News/Post">
                    <Image>
                        <path>/</path>
                    </Image>
                    <Default />
                </system-data-structure>
            </system-page>
        </calling-page>
    </system-index-block>
    

    I see how to get things like date and author. How do I get my metadata date?

  4. 2 Posted by Ryan Griffith on 17 May, 2016 07:01 PM

    Ryan Griffith's Avatar

    Hi,

    To confirm, are you referring to the created-on timestamp? If so, check your Index Block and make sure the System Metadata option is checked.

    Please let me know if you have any questions.

    Thanks!

  5. 3 Posted by webadmin on 17 May, 2016 07:18 PM

    webadmin's Avatar

    No, I can get the created-on timestamp. I can't figure out how to get the date value out of the dynamic-metadata item (in this case, 1-1-2008).

  6. 4 Posted by Ryan Griffith on 17 May, 2016 07:32 PM

    Ryan Griffith's Avatar

    Ah, I see.

    You can use the XPath Tool to obtain the value of the field:

    #set ($date = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/dynamic-metadata[name = 'Date']/value"]))
    $date.value
    

    Alternatively, you can use the Cascade API and bypass the Index Block all-together:

    $currentPage.getDynamicMetadataField("Date").textValue
    

    Note: if you need to do any sort of formatting (based on either example above), you will need to convert the string into a Date object using the Date Tool:

    #set ($dateObj = $_DateTool.toDate("M-d-yyyy", $date))
    $_DateTool.format("MMM dd, yyyy", $dateObj)
    

    Please let me know if you have any questions.

    Thanks!

  7. 5 Posted by webadmin on 17 May, 2016 07:58 PM

    webadmin's Avatar

    Neither seem to be working. You have an extra ] in the first set of code, but I removed it. In both cases, all I get for output is the variable name itself.

  8. 6 Posted by Ryan Griffith on 17 May, 2016 08:03 PM

    Ryan Griffith's Avatar

    My apologies for the extra square bracket in the snippet I provided.

    The date formatting snippet I provided was meant to be an example, so it won't work if you are using the XPath snippet. To get it working, try the following:

    #set ($date = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/dynamic-metadata[name = 'Date']/value"))
    #set ($dateObj = $_DateTool.toDate("M-d-yyyy", $date.value))
    $_DateTool.format("MMM dd, yyyy", $dateObj)
    

    Please let me know if you have any questions.

    Thanks!

  9. 7 Posted by webadmin on 17 May, 2016 08:06 PM

    webadmin's Avatar

    It's still not working, but I think I see why. I'm trying to get the data out of the current page WITHOUT having an index associated with it. When I get the image URL, for example, I don't have to assign an index page to it. I'm able to access it directly by this code:

    #set ($content = $_XPathTool.selectSingleNode($contentRoot, '//system-data-structure'))
    #set ($imgsrc = "nothing")
    #set ($imgsrc = $content.getChild('Image').getChild('link').value)
    

    But when I use the code you gave me, it looks like it's not returning anything.

  10. 8 Posted by Ryan Griffith on 17 May, 2016 08:15 PM

    Ryan Griffith's Avatar

    Ah, I see.

    You will need to attach an Index Block to the region in order to access the rest of the data. The DEFAULT region is slightly different in that if you do not apply an Index Block the $contentRoot variable will content the page's Structured Data, or page-xhtml if there is no Data Definition applied.

    Please let me know if you have any questions.

    Thanks!

  11. 9 Posted by webadmin on 18 May, 2016 07:46 PM

    webadmin's Avatar

    So, does this mean that I'll have to attach an index to each page? Will it have to be one made for that page, or can I attach one for the whole site?

  12. 10 Posted by Ryan Griffith on 18 May, 2016 08:22 PM

    Ryan Griffith's Avatar

    Hi,

    Correct, if you would like to re-use this code you would need to ensure the region(s) that use the Format have an Index Block applied.

    If you would like to avoid needing to use an Index Block, you can use the $currentPage and Cascade API to get the field. The following should give you the value of the field:

    $currentPage.metadata.getDynamicField("Date").textValue
    

    Please let me know if you have any questions.

    Thanks!

  13. 11 Posted by webadmin on 18 May, 2016 09:12 PM

    webadmin's Avatar

    The $currentPage thing is what I'd like to do, but it doesn't seem to be working. I'm attempting to use these lines of code to read in the "Date" field:

    #set ($date = $currentPage.metadata.getDynamicField("Date").textValue)
    Date is $date <br />
    

    All it's outputting is "Date is $date".

  14. 12 Posted by Ryan Griffith on 19 May, 2016 11:37 AM

    Ryan Griffith's Avatar

    My apologies, I was getting structured data fields mixed up with metadata fields.

    When you have a moment, change .textValue to .value.

    Please let me know if you have any questions.

    Thanks!

  15. 13 Posted by webadmin on 19 May, 2016 12:54 PM

    webadmin's Avatar

    Perfect. That was it! Thanks!

  16. 14 Posted by Ryan Griffith on 19 May, 2016 01:49 PM

    Ryan Griffith's Avatar

    Thank you for following up, I am glad to hear the adjustment did the trick.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  17. Ryan Griffith closed this discussion on 19 May, 2016 01:49 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac