Create Custom Fields For A Custom Entity In The Control Panel

Customization is at the core of Liferay, as it offers component classes for enhancing its functionality.

30 Mar 2024

Indeed, you read correctly—from the control panel, you can make custom fields for custom entities. But hold on, it's not OOTB. For this, you must write a single component class.

Here you go.

  • In your module, create a component class and include the component declaration listed below.

    @Component(property = "javax.portlet.name=" + ObjectPortletKeys.OBJECT_DEFINITIONS, service = CustomAttributesDisplay.class)

  • Ensure that the class "BaseCustomAttributesDisplay" is extended by yours.

    public class ObjectDefinitionCustomAttributesDisplay extends BaseCustomAttributesDisplay
  • Include the following methods in the class.

    @Override

      public String getClassName() {

        return ObjectDefinition.class.getName();//for this class it will add custom field section in control panel

      }

      @Override

      public String getIconCssClass() {

        return "relationship";

      }

  • This is how your class will now appear.

    import org.osgi.service.component.annotations.Component;

    import com.liferay.expando.kernel.model.BaseCustomAttributesDisplay;

    import com.liferay.expando.kernel.model.CustomAttributesDisplay;

    import com.liferay.object.constants.ObjectPortletKeys;

    import com.liferay.object.model.ObjectDefinition;

    @Component(property = "javax.portlet.name="

        + ObjectPortletKeys.OBJECT_DEFINITIONS, service = CustomAttributesDisplay.class)

    public class ObjectDefinitionCustomAttributesDisplay extends BaseCustomAttributesDisplay {

      

      @Override

      public String getClassName() {

        return ObjectDefinition.class.getName();

      }

      @Override

      public String getIconCssClass() {

        return "relationship";

      } 

    }

     
  • Deploy this portlet now, then use the control panel to examine the custom field area. There, the object definition will be shown.