Search for Resources and Solutions

Create a widget for Midrub Dashboard

In this article i will explain how to create a new widget in the Midrub Dashboard App. You can create your widgets by using the Midrub Apps and them will be displayed for users. You can add even the option in admin to allow to admin to enable or disable the widgets.

The Dashboard's widgets are standalone classes which depends on an interface. You can create the widget class even without the interface.

The interface code:



interface Widgets {

    /**
     * The public method display_widget will return the widget html
     * 
     * @since 0.0.7.0
     * 
     * @param integer $user_id contains the user's id
     * @param string $plan_end contains the plan's end period time
     * @param object $plan_data contains the user's plan's data
     * 
     * @return array with widget html
     */ 
    public function display_widget( $user_id, $plan_end, $plan_data );
    
    /**
     * The public method widget_helper processes the widget content
     * 
     * @since 0.0.7.0
     * 
     * @param integer $user_id contains the user's id
     * @param string $plan_end contains the plan's end period time
     * @param object $plan_data contains the user's plan's data
     * 
     * @return array with widget's content
     */ 
    public function widget_helper( $user_id, $plan_end, $plan_data );
    
    /**
     * The public method widget_info contains the widget options
     * 
     * @since 0.0.7.0
     * 
     * @return array with widget information
     */ 
    public function widget_info();

}

The namespace of the interface is:


namespace MidrubBase\User\Apps\Collection\Dashboard\Interfaces;

This widgets format is used only by the Dashboard's app. I won't use it to create other Dashboard's apps because i've learned a more professional way.

The widget's classes should be located in app folder -> widgets - otherwise won't be called.

Please remember, widget class should be the file name. Don't worry if other widgets in other apps has same name.

The structure of the widgets class is very simply:

  • display_widget - is the most important method which will be called to get the widget content. 
  • widget_helper - is optionally and you can use it to create the widget's content.
  • widget_info - has the widget's information with name, slug and widget's order.


The display_widget should return an array with two values:

  • widget - html content.
  • order - widget order in the dashboard.


In the video below you can see how to create a widget:



Was this article helpful?