Extending a Widget in the Custom Backoffice-Based Application
In Backoffice Framework, you have access to the widgets library that you can easily use out-of-the-box. However, you may want to customize the default widget behavior or aspects of its design. That is the reason why you are able to extend a widget within the Backoffice Framework.
The below steps describe how to extend the Explorer Tree widget available in Backoffice application. First of all, you need to create a custom Backoffice-based application which you can customize then. If you go through the steps in the tutorial you will modify widget's design, add a socket output event and redefine the controller class.
Creating a Custom Backoffice Extension
To extend a widget, you need to start with creating custom Backoffice extension.
Procedure
- Open a command prompt.
- Navigate to the <HYBRIS_BIN_DIR>/platform directory and call setantenv.bat.
- In the same directory call ant extgen.
- Follow the instructions that show up on the screen:
- Type ybackoffice to use the proper template and press ENTER.
- Type myextension for extension name and press ENTER.
- Type org.myextension for package name and press ENTER.
- Decide if you want a sample widget to be generated. If not, then do not forget to manually create a widgets folder in the myextension/backoffice/resources directory. Here you can add a new directory for each component that you create.
- Decide if you want a sample style sheet to be generated. For more details, read Replacing Styles of Backoffice Application. New extension is generated in the following directory: <HYBRIS_BIN_DIR>/custom
- Add the new extension to your localextensions.xml file ( config folder). To load all custom extensions automatically, add the following line to localextensions.xml, at the beginning of the file after other path statements: <path autoload="true" dir="${HYBRIS_BIN_DIR}/custom"/>
Building Your Project
Start with your project in the Backoffice application.
Procedure
- Open a command prompt.
- Navigate to the <HYBRIS_BIN_DIR>/platform directory.
- Make sure that a compliant version is used:
- On the Windows operating system, call the <HYBRIS_BIN_DIR>/platform/setantenv.bat file. Do not close the command prompt after this call as the settings are transient and are lost once the command prompt is closed.
- On the Unix operating system, call the <HYBRIS_BIN_DIR>/platform/setantenv.sh file, such as: . ./setantenv.sh.
- Call ant clean all to build SAP Commerce Cloud.
Starting the Application Server
After building you project in Backoffice, you can start working in your application.
Procedure
- In a command prompt, navigate to the <HYBRIS_BIN_DIR>/platform directory.
- Type hybrisserver.bat and press ENTER.
- Open the Backoffice Application using, for example, this link http://localhost:9001/backoffice. The newly created myextension extension is just a place where you should create your custom components.
Extending the Widget Definition
To extend a default widget, define a new widget within the definition.xml file.
Procedure
Overriding the Widget Definition
Backoffice Framework provides you a possibility to override your widgets' definitions.
To override a particular widget's definition, you need to create a separate widget definition. The widget definition should contain the ID of the widget to override set in the extends attribute and should not contain its own ID.
- widget controller
- default title
- keywords
You can change widget input and output sockets. Then, the new sockets are added. This action does not remove parent sockets, but uses the merging mechanism instead of overriding.
You can also change widget settings. New widget settings are added to the parent settings. This action does not remove parent settings, but uses the merging mechanism instead of overriding. See example below:
<widget-definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/widget-definition.xsd" extends="com.hybris.cockpitng.widgets.compare"> <name>WinnerCompareBackoffice Sample Widget</name> <controller class="com.winnercomparebackoffice.widgets.WinnerCompareBackofficeController" /> </widget-definition>
Modifying the Widget Design
To modify a widget's design you need to register an additional CSS file for your newly created widget, where you can add CSS-specific changes.
Procedure
Extending the Widget Controller
Start with your widget controller class.
Context
In this tutorial, you override a controller, rather than define a new widget that differs only with a controller class. You may add an id attribute to the widget definition shown below to define a new widget and leave the existing one without any changes.
Procedure
Redefining the Widget Sockets
To add new sockets to your custom widget, add the corresponding socket declaration to the definition.xml file.
Context
In this tutorial, you add sockets to the widget that already exists. If you wish, you can add an id attribute to the snippet shown in these steps to define a new widget and leave the existing one without any changes.
Procedure
definition.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget-definition extends="com.hybris.cockpitng.widgets.common.explorertree"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/widget-definition.xsd">
<name>Extended Explorer Tree</name>
<defaultTitle>Extended Explorer Tree</defaultTitle>
...
<sockets>
<input id="myCustomInput" type="java.lang.String" />
<input id="setTreeContext" type="java.util.Collection" />
<output id="myCustomOutput" type="java.lang.String" />
</sockets>
...
</widget-definition>
Rebuilding Your Project
Rebuild you project in Backoffice to include all the configuration changes.
Procedure
- Open a command shell.
- Navigate to the <HYBRIS_BIN_DIR>/platform directory.
- Call ant clean all to build SAP Commerce Cloud.
Restarting the Application Server
Start the Backoffice application again after the rebuild.
Procedure
- Open a command prompt, and navigate to the <HYBRIS_BIN_DIR>/platform directory.
- For Windows type hybrisserver.bat or ./hybrisserver.sh on Unix system and press ENTER.
- Open the Backofficeapplication using, for example, this link http://localhost:9001/backoffice. The newly created myextension extension is just a place where you should create your custom components.
Verifying the Definition.xml File
Check the custom widget settings against the definition.xml file of your Backoffice project.
Procedure
- Press F4 on your keyboard.
- Find Explorer Tree widget.
- Click the edit icon
. - Verify the definition.xml file for your widget: click About tab in the pop-up window.