A plugin can add its own widgets to the dashboards with the filter app_filter_dashboard_widgets.
Add a widget
The filter gets the widget list and a context. The context tells which dashboard is being made:
- user_type: staff or client. It is client for client contacts and when an admin edits the default client dashboard.
- client_id: the client of the login contact. It is 0 when an admin edits the default client dashboard.
Return only the widgets this user may see. Use a function for widget_view: the widget is then made only when it is shown on the dashboard.
app_hooks()->add_filter('app_filter_dashboard_widgets', function ($widgets, $context = array()) {
if (get_array_value($context, "user_type") === "client") {
$widgets[] = array(
"widget" => "my_plugin_client_widget",
"widget_view" => function ($context) {
$client_id = get_array_value($context, "client_id");
return view("My_Plugin\Views\client_widget", array("client_id" => $client_id));
}
);
}
return $widgets;
}, 10, 2);
The widget name is also its language key: add $lang["my_plugin_client_widget"] to your language file. It is the title in the widget list.
In the dashboard editor, plugin widgets are shown in the Plugins group of the widget list. Users can search them by title.
A plain HTML string still works for widget_view, but it is made on every dashboard page, even when the widget is not used.
Use the same look as the app widgets
Small widget:
List widget: call list_widget() with a title, a feather icon, a view all link and rows. Rows have title, url, subtitle and value. Text is escaped for you.
echo list_widget(array(
"title" => "Recent sales",
"icon" => "key",
"view_all_url" => get_uri("my_plugin"),
"rows" => array(
array("title" => "Product", "url" => get_uri("my_plugin/view/1"), "subtitle" => "Client name", "value" => "$10.00")
)
));
A row with url is one big link. If a row needs its own buttons (for example a download menu), leave out url. Then use title_url to link only the title, and put the buttons in value_html.
array("title" => "Product", "title_url" => get_uri("my_plugin/view/1"), "subtitle" => "Regular license", "value_html" => $download_menu)
More options for list_widget():
- full_height: the list keeps the same height with few or no rows.
- empty_icon and empty_message: an icon and a message in the middle of an empty list.
- empty_link_url and empty_link_text: a link under the empty message (for example to the store).
- page_size: shows this many rows first. A "Load more" line shows the next ones. Good for lists without a details page.
- header_html: HTML at the right of the title, for example a status badge.
- footer_html: HTML under the rows, for example a note and a button.
- card_class: extra CSS classes for the card.
- Row details_html: HTML under the subtitle, not cut, for example notes.
echo list_widget(array(
"title" => "Downloads",
"icon" => "package",
"rows" => $rows,
"full_height" => true,
"page_size" => 15,
"empty_icon" => "package",
"empty_message" => "You don't have any downloads yet.",
"empty_link_url" => get_uri("store"),
"empty_link_text" => app_lang("go_to_store")
));