accessing 'hippo:name' of a Document

107 views
Skip to first unread message

Brian Street

unread,
Mar 3, 2017, 6:22:10 PM3/3/17
to Hippo Community
When creating a new document, the author is prompted for:
* Name (stored as @hippo:name property)
* URL name (JCR node name of the document) - optional; usually derived from lowercase of the Name, with dashes instead of spaces & probably URL encoding also
* document type (which type of document)

In the document bean, I would like to return the authored "Name" of the document so I don't have to repeat it as a field inside the custom document.  Is there a recommended approach to re-using the property?

Example:
/content/documents/my-site/content/my-doc
    @hippo:name = "My Cool Title"
    \my-doc
        @hippo:availability
    \my-doc
        @hippo:availability = "preview"
    \my-doc
        @hippo:availablity = "live"
        @myns:str1 = "A"
        @myns:str2 = "B"
        @myns:long = 123

public class MyDocument extends HippoDocument {
    public String getStr1() { return getProperty("myns:str1"); }
    public String getStr2() { return getProperty("myns:str2"); }
    public String getLong() { return getProperty("myns:long"); }

    public String getMyTitle() {
        return getProperty("hippo:name");  // <= null/"" because the live node doesn't have this property
        return getProperty("../hippo:name"); // <= null/""; looking for shortcut relative path that worked
        return getParentBean().getProperty("hippo:name"); // <= "Content", the @hippo:name property of the node ABOVE the document
        try {
            return getNode().getParent().getProperty("hippo:name").getString(); // <= returns desired "My Cool Title"
        } catch (RepositoryException re) { return re.getMessage(); }
    }
}

Obviously, dropping to JCR API is undesired because of exceptions, but it was the only way I found to obtain the property I was looking for without duplicating it inside another field of the custom document.

I _happen_ to be rendering through Freemarker templates, but believe that has nothing to do with it.  Calling document.getName(), document.getDisplayName() all return the wrong thing (but I don't understand the difference in them).  Calling document.getPath() returns the path to the live node (/content/documents/my-site/content/my-doc/my-doc).

Also, I am on Hippo v11.1, but I don't believe that is important to the question.

Woonsan Ko

unread,
Mar 3, 2017, 7:11:04 PM3/3/17
to hippo-c...@googlegroups.com
Hi Brian,

Please see my comments inline.

On Fri, Mar 3, 2017 at 6:22 PM, Brian Street <brian.c...@gmail.com> wrote:
When creating a new document, the author is prompted for:
* Name (stored as @hippo:name property)
* URL name (JCR node name of the document) - optional; usually derived from lowercase of the Name, with dashes instead of spaces & probably URL encoding also
* document type (which type of document)

In the document bean, I would like to return the authored "Name" of the document so I don't have to repeat it as a field inside the custom document.  Is there a recommended approach to re-using the property?
Yes, you don't need to implement anything new in your content bean, but you should simply use HippoBean#getDisplayName() for the "Name (stored in ../@hippo.name property)". That's the recommended/supported way. And, #getDisplayName() is supposed to read the parent handle node's @hippo.name property even when you are reading it in a document variant node / bean.
 

Example:
/content/documents/my-site/content/my-doc
    @hippo:name = "My Cool Title"
    \my-doc
        @hippo:availability
    \my-doc
        @hippo:availability = "preview"
    \my-doc
        @hippo:availablity = "live"
        @myns:str1 = "A"
        @myns:str2 = "B"
        @myns:long = 123

public class MyDocument extends HippoDocument {
    public String getStr1() { return getProperty("myns:str1"); }
    public String getStr2() { return getProperty("myns:str2"); }
    public String getLong() { return getProperty("myns:long"); }

    public String getMyTitle() {
        return getProperty("hippo:name");  // <= null/"" because the live node doesn't have this property
        return getProperty("../hippo:name"); // <= null/""; looking for shortcut relative path that worked
        return getParentBean().getProperty("hippo:name"); // <= "Content", the @hippo:name property of the node ABOVE the document
        try {
            return getNode().getParent().getProperty("hippo:name").getString(); // <= returns desired "My Cool Title"
        } catch (RepositoryException re) { return re.getMessage(); }
    }
}

Obviously, dropping to JCR API is undesired because of exceptions, but it was the only way I found to obtain the property I was looking for without duplicating it inside another field of the custom document.
Using jcr property manually is not recommended. #getDisplayName() should work.
 

I _happen_ to be rendering through Freemarker templates, but believe that has nothing to do with it.  Calling document.getName(), document.getDisplayName() all return the wrong thing (but I don't understand the difference in them).  
#getName() is supposed to return the JCR node name. e.g, "my-doc", and #getDisplayName() is supposed to return the node's handle (parent) node's @hippo:name property.

 
Calling document.getPath() returns the path to the live node (/content/documents/my-site/content/my-doc/my-doc).
Right.
 

Also, I am on Hippo v11.1, but I don't believe that is important to the question.
@hippo:name property has been introduced since Hippo v11. So, it's a bit related.

The way how it works when invoking document#getDisplayName() is as follows:
- ...
- it checks whether the current node (e.g, live variant node) is type of "hippo:named" mixin type. Obviously the live variant node must not be, but its parent handle node must be.
- if the current node is not type of "hippo:named", then it gets the parent node. If the parent node is type of "hippo:named", then it returns @hippo:name property as expected.
- otherwise, the return will be the same as #getName().

If you see a different behavior than expected, then please check the following:
- the live variant node must not have a hippo:named mixin type by itself.
- but its parent handle node must must have hippo:named mixin type.
- the parent handle node must have hippo:name property.

HTH,

Woonsan
 

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-community@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-community+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.



--
71 Summer Street, 2nd Floor, Boston, MA 02110
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466

Brian Street

unread,
Mar 6, 2017, 11:18:17 AM3/6/17
to Hippo Community
The live variant node does NOT have the hippo:named mixin while its parent node DOES.  So based on your requirements, it should work.  I suspect that in going through translations (contentBean.getAvailableTranslations()/translations.getTranslation(localeStr)) we lose this information.

Woonsan Ko

unread,
Mar 6, 2017, 11:29:10 AM3/6/17
to hippo-c...@googlegroups.com
On Mon, Mar 6, 2017 at 11:18 AM, Brian Street <brian.c...@gmail.com> wrote:
The live variant node does NOT have the hippo:named mixin while its parent node DOES.  So based on your requirements, it should work.  I suspect that in going through translations (contentBean.getAvailableTranslations()/translations.getTranslation(localeStr)) we lose this information.
org.hippoecm.hst.content.beans.standard.AvailableTranslations#getTranslation(String) returns a HippoBean. Do you get null from #getTranslation(localeStr)? Or Do you get an unexpected value from #getTranslation(localeStr).getDisplayName()? In the latter case, you can investigate why it happens based on my explanation. You can print the node path (#getTranslation(localeStr).getPath()), too.

Regards,

Woonsan
 

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-community@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-community+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages