Get checkbox value using block locator tool

sbarre5's Avatar

sbarre5

16 Feb, 2015 07:22 PM

I am allowing users to add multiple blocks to a page and i'm using the locator tool to extract the content from the block. On each block, I have given the user a checkbox for a few options - i.e. if they want a header, if they want to include a picture, etc. In my velocity, I need to check those options and display things accordingly. It seems I'm getting into my block correctly but I can't run a check on the checkbox values.

Sample XML for Block I'm trying to extract the information from (include-header is the checkbox):

<system-data-structure>
    <page-element>
        <include-header>
            <value>Yes</value>
        </include-header>
        <content-type>Text (w/ Supporting Media)</content-type>
        <header>
            <text>This is the header</text>
            <heading-level>Heading 2</heading-level>
        </header>
    </page-element>
</system-data-structure>

When I run the properties tool I get:

Object type: com.hannonhill.cascade.api.adapters.XHTMLDataDefinitionBlockAPIAdapter
Properties:
 - lastModified: Date
 - dataDefinitionPath: String
 - parentFolder: Folder
 - identifier: PathIdentifier
 - getStructuredDataNode(String): StructuredDataNode
 - isUserCanWrite(String): boolean
 - structuredData: StructuredDataNode[]
 - currentUserCanWrite: boolean
 - createdOn: Date
 - metadata: Metadata
 - folderOrder: int
 - parentFolderIdentifier: PathIdentifier
 - createdBy: String
 - identifer: Identifier
 - path: String
 - lastModifiedBy: String
 - currentUserCanRead: boolean
 - isUserCanRead(String): boolean
 - hideSystemName: boolean
 - xHTML: String
 - siteName: String
 - siteId: String
 - xHTMLAsXMLElement: Element
 - name: String
 - getStructuredDataNodes(String): StructuredDataNode[]

I've tried the following but everything is still saying object is null:

#set ( $headings = $local_block.getStructuredDataNodes("page-element/include-header"))
#set ( $headings = $local_block.getStructuredDataNode("page-element/include-header/value").value)
#set ( $headings = $local_block.getStructuredDataNode("page-element/include-header").value)
#set ( $headings = $local_block.getStructuredDataNode("page-element/include-header/value").textValue)
#set ( $headings = $local_block.getStructuredDataNode("page-element/include-header").textValue)
  1. 1 Posted by Ryan Griffith on 16 Feb, 2015 07:53 PM

    Ryan Griffith's Avatar

    Hi,

    For checkbox, radio and multi-select fields, you will want to use is the textValues property for the field, which will return an array of each checked/selected value. You can use the Property Tool to see the available properties for the StructuredDataNodeAPIAdapter.

    Note: there is a known defect in which the first item in the returned array is empty, so the array basically starts at index 1. The following should return your value:

    $local_block.getStructuredDataNode("page-element/include-header").textValues.get(1)
    

    Also, you may want to check the size of the array prior to attempting to use .get(1) to avoid an IndexOutOfBounds error. Because of the known defect, you basically need to check to see if the size is greater that 1 (or equal to 2 if you know for a fact there will only ever be one checkbox):

    #set ($headings = $local_block.getStructuredDataNode("page-element/include-header").textValues)
    #if ($headings.size() == 2)
        ## ... do something with the value "Yes"
        $headings.get(1)
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by sbarre5 on 16 Feb, 2015 08:01 PM

    sbarre5's Avatar

    I didn’t even know textValue(s) was even a thing. As usual, worked like a charm!

    Saundra Barrett
    Senior Web Developer
    Emory University, Web Design Group
    Office: 404-727-3437
    Cell: 404-670-1238

  3. 3 Posted by Ryan Griffith on 16 Feb, 2015 08:16 PM

    Ryan Griffith's Avatar

    Thank you for following up, Saundra. I am glad to hear the proposed change did the trick.

    I didn’t even know textValue(s) was even a thing

    You were definitely on the right track when you tried textValue, since it is technically a "text" field. The difference is you can have multiple checkbox "items" per field, so you need to account for that by using textValues which will return an array.

    Perhaps an improvement could be to allow for textValue to return the checked/selected value for checkboxes and multi-selects if only one value is present, or for radio fields since you can only ever have one value. I went ahead and logged an improvement for this functionality. Feel free to use this link to track the progress of this improvement or keep an eye out for it on our Release Notes site.

    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!

  4. Ryan Griffith closed this discussion on 16 Feb, 2015 08:16 PM.

  5. Ryan Griffith re-opened this discussion on 02 Jun, 2016 02:27 PM

  6. Ryan Griffith closed this discussion on 02 Jun, 2016 02:27 PM.

Comments are closed, but you can start a new discussion.

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