I am a beginner in the Sap Hybris. I want to understand the backoffice extension and how to create our custom widget. Just a small example to understand with the different steps.
I'm going to clear the steps to create a widget
Creating a Widget
To create a widget, you need to start by creating a widget definition. The definition of a widget is provided in definition.xml
file.
Procedure
In myextension/backoffice/resources/widgets
directory create a new folder called mysearch.
In the mysearch folder, create a definition.xml file.
Add information about the search widget.
You can provide information like name, description, default title, author, and version. Each widget must have a unique ID made up of the extension and widget names. For this tutorial, the widget ID is org.myextension.widgets.mysearch.
definition.xml :
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<widget-definition id="org.myextension.widgets.mysearch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/widget-definition.xsd">
<name>My Search</name>
<description>My own search widget.</description>
<defaultTitle>Search</defaultTitle>
<author>Me</author>
<version>0.1</version>
</widget-definition>
Creating a Widget View :
The view of this widget is defined in a ZK ZUL file, named after the last part of the widget ID as specified in the definition.xml (in this case mysearch.zul).
Context
In the mysearch.zul file, you define all frontend components. For the search widget, you need two components: a text box and a button.
Procedure
mysearch.zul
<widget xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.zkoss.org/2005/zul">
<style src="${wr}/default.css"/>
<div>
<hlayout>
<textbox id="searchInput"/>
<button id="searchBtn" label="Search"/>
</hlayout>
</div>
</widget>
Deploying the Widget
Now, the newly created widget needs to get deployed.
Procedure
Include your new extension:
a. Open the <HYBRIS_CONFIG_DIR>/localextensions.xml file.
b. Add the following line
<extension name="myextension" />
c. Save the file.
Build your project:
Call ant clean all to build SAP Hybris Commerce and start server
Adding the Widget to an Application
After deploying a widget, you need to add the widget to an application.
Implementing the Search Service
The widget requires implementing some logic. As the widget is responsible for search, the SearchService is to be implemented.
Context
Before creating controller, you must add a SearchService, which is used by your search widget.
Procedure
SearchService.java
package org.myextension;
import java.util.ArrayList;
import java.util.List;
public class SearchService {
public List<String> search(final String text) {
List<String> result = new ArrayList<String>();
result.add(text);
result.add("Hello");
result.add("Cockpit NG");
result.add("Developer");
return result;
}
}
myextension-backoffice-spring.xml
<bean id="searchService" class="org.myextension.SearchService"/>
Creating a Controller
The newly created widget requires a widget controller.
Context
As you have not defined any action for the Search button, nothing happens if it is clicked. For this you need to create a controller.
Procedure
The controller should extend DefaultWidgetController. Name it MySearchController.
Add the implementation for the following actions:
a. When a search query is typed in the text box, it should trigger the search.
b. When the Search button is pressed, the search for the search query should be executed.
MySearchController.java
package org.myextension.widgets.mysearch;
import java.util.List;
import org.myextension.SearchService;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import com.hybris.cockpitng.annotations.ViewEvent;
import com.hybris.cockpitng.util.DefaultWidgetController;
public class MySearchController extends DefaultWidgetController {
private Textbox searchInput;
@WireVariable
private SearchService searchService;
@ViewEvent(componentID = "searchBtn", eventName = Events.ON_CLICK)
public void doSearch() throws InterruptedException {
List<String> result = searchService.search(searchInput.getText());
Messagebox.show(result.get(0));
}
}
definition.xml
Rebuild the system like you did in the Deploying the Widget procedure.
Click the Search button.
A pop-up message appears with the same text that you typed into the text box.