Skip to content
OpenCms documentation
OpenCms documentation

Hiding schema elements

OpenCms allows hiding of elements in the content editor for users of specific groups. Which element is hidden for which group is specified in the content type definition. If the default options for hiding are not sufficient, it is possible to implement a custom visibility handler.

Which schema element is visible to which users can be defined with a <Visibility> field setting in the schema definition (XSD) in the section xsd:annotation/xsd:appinfo. A possible configuration could look like this:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/>

    <xsd:element name="HideExamples" type="OpenCmsHideExamples"/>

    <xsd:complexType name="OpenCmsHideExamples">
        <xsd:sequence>
            <xsd:element name="HideExample" type="OpenCmsHideExample" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="OpenCmsHideExample">
        <xsd:sequence>
            <xsd:element name="NotForEveryOne" type="OpenCmsString" />
            <xsd:element name="ForNobody" type="OpenCmsHtml" />
            <xsd:element name="ForEveryOne" type="OpenCmsHtml" />
        </xsd:sequence>
        <xsd:attribute name="language" type="OpenCmsLocale" use="required"/>
    </xsd:complexType>

    <xsd:annotation>
        <xsd:appinfo>
            <FieldSettings>
                <Setting>
                    <PropertyName>NotForEveryOne</PropertyName>
                    <Visibility>Administrators|Projectmanagers|ROLE.GALLERY_EDITOR</Visibility>
                </Setting>
                <Setting>
                    <PropertyName>ForNobody</PropertyName>
                    <Visibility>hidden</Visibility>
                </Setting>
            </FieldSettings>
        </xsd:appinfo>
    </xsd:annotation>

</xsd:schema>

The visibility setting Administrators|Projectmanagers|ROLE.GALLERY_EDITOR defined for the NotForEveryOne field defines that the element named NotForEveryOne is only visible for members of one of the defined groups or roles. The <Visibility> setting internally uses the the default visibility handler. The default handler takes a pipe (|)-separated list of group names and role identifiers. For users that are members of at least one of the listed groups or that have at least one of the listed roles, the content element is displayed in the editor. For all other users it is hidden.

Consequently, if you leave the setting empty or you define an arbitrary value such as hidden, that is not a group name or role name, the element is never displayed as for the example's ForNobody field.

If you want to use a custom visibility handler, you have to choose a <FieldVisibility> field setting which has two child elements <Class> and <Params> where <Class> defines the handler class and <Params> an arbitrary configuration string.

To implement a custom visibility handler, implement the I_CmsXmlContentVisibilityHandler interface and in that the method isValueVisible(..). The method returns a boolean value indicating whether the element should be shown or not. The method's arguments provide various contextual information. Additionally, a configuration string is provided and you are completely free about how to parse and use it.

<xsd:annotation>
    <xsd:appinfo>
        <FieldSettings>
            <Setting>
                <PropertyName>SomeField</PropertyName>
                <FieldVisibility>
                    <Class>org.example.my.handler.VisibilityHandler</Class>
                    <Params>param1,param2,param3</Params>
                </FieldVisibility>
            </Setting>
        </FieldSettings>
    </xsd:appinfo>
</xsd:annotation>