Support and Documentation

Creating

You can create widgets that appear in the content edit page by extending the abstract class ContentEditWidget. That abstract class includes three abstract methods that you must override, all of which support displaying read-only data. For information about creating content edit widgets that can update data, see Creating updating content edit widgets.

Step 1: Declare a custom content edit widget

import com.psddev.cms.tool.ContentEditWidget;

public class CurrentTimesWidget extends ContentEditWidget {

}

Step 2: Display the widget's header

You display the custom widget's header by implementing the method getHeading.

public String getHeading(ToolPageContext page, Object content) {
    /* Return string appearing in widget's title bar. */
}

Step 3: Display the widget's body

You display the custom widget's body by implementing the method display.

public void display(ToolPageContext page, Object content, ContentEditWidgetPlacement placement) throws IOException {
    /* Use page.write methods to display the widget's body. */
}

Step 4: Display custom widget in content edit page

The following snippet shows an entire class for displaying the custom widget Current Time Zones in the content edit page. The custom widget lists several cities and their current time.

Example 41. Implementing a custom widget in content edit page
import com.psddev.cms.tool.*;

public class CurrentTimesWidget extends ContentEditWidget { 1

    @Override
    public void display(ToolPageContext page, Object content, ContentEditWidgetPlacement placement) throws IOException { 
        Map<String, String> timeZoneIdentifiers = new HashMap<>(); 2
        timeZoneIdentifiers.put("New York", "America/New_York");
        timeZoneIdentifiers.put("Los Angeles", "America/Los_Angeles");
        timeZoneIdentifiers.put("Mexico City", "America/Mexico_City");

        page.writeStart("table");

            page.writeStart("tr"); 3
                page.writeStart("th");
                    page.writeHtml("City");
                page.writeEnd(); /* th */
                page.writeStart("th");
                    page.writeHtml("Time");
                page.writeEnd(); /* th */
            page.writeEnd(); /* tr */

            for (String myTimeZone : timeZoneIdentifiers.keySet()) { 4
                page.writeStart("tr");
                    page.writeStart("td");
                        page.writeHtml(myTimeZone);
                    page.writeEnd(); /* td */
                    page.writeStart("td");
                        String localTime = displayTime(timeZoneIdentifiers.get(myTimeZone));
                        page.writeHtml(localTime);
                    page.writeEnd(); /* td */
                page.writeEnd(); /* tr */
            }
        page.writeEnd(); /* table */
    }

    public ContentEditWidgetPlacement getPlacement(ToolPageContext page, Object content) { 5
        return ContentEditWidgetPlacement.BOTTOM;
    }

    public String getHeading(ToolPageContext page, Object content) { 6
        return "Current Times";
    }

    private String displayTime(String timeZoneIdentifier) {
        /* 
            Returns local time in hh:mm format based on
            passed time zone identifier.
        */
    }
}

1

Declares the class CurrentTimesWidget. Objects instantiated from this class appear as Current Times Widget in the dashboard.

2

Instantiates a HashMap of time zones.

3

Writes a table header in the widget's body.

4

Loops through each time-zone record. For each record, call the method displayTime to retrieve and then print the time zone's current time.

5

Positions the widget under the content edit form. For details about positioning a widget on the content edit form, see Position.

6

Displays the widget's heading.



Based on the previous snippet, the custom widget Current Times appears at the bottom of the content edit page.

custom-widget-content-edit-form.svg

By default, Brightspot displays custom widgets on the content edit page for all content types. You can suppress a custom widget for individual content types; for details, see Hiding.