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

  1. Open a command prompt.
  2. Navigate to the <HYBRIS_BIN_DIR>/platform directory and call setantenv.bat.
  3. In the same directory call ant extgen.
  4. Follow the instructions that show up on the screen:
    1. Type ybackoffice to use the proper template and press ENTER.
    2. Type myextension for extension name and press ENTER.
    3. Type org.myextension for package name and press ENTER.
    4. 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.
    5. 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
  5. 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

  1. Open a command prompt.
  2. Navigate to the <HYBRIS_BIN_DIR>/platform directory.
  3. 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.
  4. 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

  1. In a command prompt, navigate to the <HYBRIS_BIN_DIR>/platform directory.
  2. Type hybrisserver.bat and press ENTER.
  3. 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

  1. Create a widget folder in the myextension/backoffice/resources/widgets path, and place the definition.xml:
    definition.xml
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <widget-definition id="com.hybris.extended.explorertree.custom" 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>            
    </widget-definition>

    The definition defines a new widget that exists along with the one defined in the extends attributes, but redefines some of its properties.

    Attribute Value Description
    id com.hybris.extended.explorertree.custom ID of a new widget, which is used in the widgets.xml file.
    extends com.hybris.cockpitng.widgets.common.explorertree A parent widget definition id.
  2. Create a CSS file in the root myextension/backoffice/resources/widgets/ widget's directory.

    Follow on of the naming conventions for the CSS file:

    • it matches the last part of widget ID
    • it’s named custom.css or default.css
  3. Add the following code snippet to your newly created file. See an example:
    @import url("/backoffice/widgetClasspathResource/widgets/explorerTreeWidget/explorertree.css"); 

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.

You can override the following elements in the widget definition:
  • 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

  1. Create a custom.css file in the root widget's directory, myextension/backoffice/resources/widgets/.
  2. Add the following code snippet to your custom.css:
    @import url("/backoffice/widgetClasspathResource/widgets/explorerTreeWidget/explorertree.css"); 
    .yw-explorerTree-filterTextbox{
        display:none
    }

    This implementation hides the filter box.

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

  1. Create a controller class for your CustomExplorerTreeController widget. This class is derived from the parent widget class ExplorerTreeController.
    package com.hybris.cockpitng.widgets.common.explorertree;
     
    import org.zkoss.zk.ui.event.Event;
    import org.zkoss.zul.Treeitem;
     
     
    public class CustomExplorerTreeController extends ExplorerTreeController
    {
       @Override
       public boolean handleTreeSelection(final Event event, final Treeitem treeItem)
       {
            //Overridden method body
       }
    }
  2. Register the newly created widget controller class in your definition.xml file.

    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">
       
     
    ...
       <controller class="com.hybris.cockpitng.widgets.common.explorertree.CustomExplorerTreeController" />
    ...
     
    </widget-definition>

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

The definition.xml merges the parent and child sockets. In this example, two input sockets and one output socket are added.

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

  1. Open a command shell.
  2. Navigate to the <HYBRIS_BIN_DIR>/platform directory.
  3. Call ant clean all to build SAP Commerce Cloud.

Restarting the Application Server

Start the Backoffice application again after the rebuild.

Procedure

  1. Open a command prompt, and navigate to the <HYBRIS_BIN_DIR>/platform directory.
  2. For Windows type hybrisserver.bat or ./hybrisserver.sh on Unix system and press ENTER.
  3. 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

  1. Press F4 on your keyboard.
  2. Find Explorer Tree widget.
  3. Click the edit icon.
  4. Verify the definition.xml file for your widget: click About tab in the pop-up window.