Custom Widgets

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
(initial document)
 
(FIELD_OTHER)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
[[Category:Extending OpenCms]]
 +
 
== Custom Widgets ==
 
== Custom Widgets ==
When creating [[XMLContent| XML content]] the input methods available with the [[XMLContent#Widgets | existing widget types]] might not suffice for the desired content.  
+
When creating [[XMLContent| XML content]] the input methods available with the [[XMLContent#Widgets | existing widget types]] might not suffice for the desired content. The existing widget types are limited to the use cases required for OpenCms. For example, they do not support dynamic configuration.
 +
 
 +
An example custom widget could for example display a drop down selection of the company's employees which is retrieved from a database.
 +
 
 +
A new widget type is created by subclassing <tt>A_CmsWidget</tt> or one of the existing widget classes. It is a good idea to take a look at them as it can save a lot of work.
 +
 
 +
To decorate an XML element with the new widget it is referenced from the layout annotation in the XML Schema just as the existing widget types. The reference can be
 +
* the FQCN (fully qualified class name) of the widget class or
 +
* an alias name just like <em>HtmlWidget</em> etc
 +
 
 +
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 +
  elementFormDefault="qualified">
 +
  <xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd" />
 +
  ...
 +
  ... your complexType definitions which include an element Manager and Developer
 +
  ...
 +
    <xsd:annotation>
 +
      <xsd:appinfo>
 +
        <layouts>
 +
          <layout element="Manager" widget="<strong>AcmeEmployeeSelectionWidget</strong>" />
 +
          <layout element="Developer" widget="<strong>com.acme.widgets.AcmeEmployeeSelectionWidget</strong>" />
 +
        </layouts>
 +
      </xsd:appinfo>
 +
  </xsd:annotation>
 +
</xsd:schema>
  
By subclassing <tt>A_CmsWidget</tt> you can easily create your own widget types. In order to make use of it it can be referenced from XSD files by its FQCN or by a alias name for the class in <tt>opencms-vfs.xml</tt>.
+
Alias names are defined in <tt>opencms-vfs.xml</tt>.
  
 
  <?xml version="1.0" encoding="UTF-8"?>
 
  <?xml version="1.0" encoding="UTF-8"?>
Line 21: Line 47:
 
  </opencms>
 
  </opencms>
  
You need to restart OpenCms to activate the change. If such an alias has been defined, the widget can be used as shown in the next listing.
+
You need to restart OpenCms to activate the change.
+
 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+
 
  elementFormDefault="qualified">
+
FIELD_MESSAGE_letoel
  <xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd" />
+
  ...
+
  ... your complexType definitions which include an element Employee
+
  ...
+
    <xsd:annotation>
+
      <xsd:appinfo>
+
        <layouts>
+
          <layout element="Employee" widget="AcmeEmployeeSelectionWidget" />
+
        </layouts>
+
      </xsd:appinfo>
+
  </xsd:annotation>
+
</xsd:schema>
+
== Custom Data Types ==
+

Latest revision as of 00:36, 17 December 2008


Custom Widgets

When creating XML content the input methods available with the existing widget types might not suffice for the desired content. The existing widget types are limited to the use cases required for OpenCms. For example, they do not support dynamic configuration.

An example custom widget could for example display a drop down selection of the company's employees which is retrieved from a database.

A new widget type is created by subclassing A_CmsWidget or one of the existing widget classes. It is a good idea to take a look at them as it can save a lot of work.

To decorate an XML element with the new widget it is referenced from the layout annotation in the XML Schema just as the existing widget types. The reference can be

  • the FQCN (fully qualified class name) of the widget class or
  • an alias name just like HtmlWidget etc
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified">
  <xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd" />
  ...
  ... your complexType definitions which include an element Manager and Developer
  ...
   <xsd:annotation>
     <xsd:appinfo>
       <layouts>
         <layout element="Manager" widget="AcmeEmployeeSelectionWidget" />
         <layout element="Developer" widget="com.acme.widgets.AcmeEmployeeSelectionWidget" />
       </layouts>
     </xsd:appinfo>
  </xsd:annotation> 
</xsd:schema>

Alias names are defined in opencms-vfs.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-vfs.dtd">
<opencms>
  <vfs>
     ...
     <xmlcontent>
       ...
       <widgets>
         ...
         <widget class="com.acme.widgets.AcmeEmployeeSelectionWidget"
           alias="AcmeEmployeeSelectionWidget" />
       </widgets>
       ...
     </xmlcontent>
  </vfs>
</opencms>

You need to restart OpenCms to activate the change.


FIELD_MESSAGE_letoel

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox