Widget

Definition

A widget is an autonomous graphical element you can add to different pages of your application. In general widgets are placed on the Home Page, or in the Summary View of a record. But it is possible to place widgets on any page of the application.

Uccello use the Laravel Widgets library for managing widgets. For more information, please refer to its documentation.

Default Widgets

By default, Uccello offers 2 widgets you can use with your modules:

  • SummaryFields

  • RelatedListWidget

These widgets allow to display some information in the Summary View of a record.

SummaryFields

Thanks to this widget you can quickly view the summary of a record, displaying the value of the fields of your choice.

Here is an example for using this widget with a module:

The data attribute must contain a json string listing the module's fields you want to display.

RelatedListWidget

This widget allows to display a Related List in the Summary View of a record. It is useful to display several Related Lists in the same page.

Here is an example for using this widget with a module:

The data attribute must contain a json string with the id of the related list you want to use.

Create widgets

Thanks to 2 tutorials we will learn to create new widgets.

Home Page widget

We will display on the home page a widget for displaying the number of Users total and only in the current domain.

  • Make the widget

You can easily create new widgets. To do this launch the following command:

This command create 2 files:

  1. app/Widgets/DashboardUserWidget.php: The widget's controller

  2. resources/views/widgets/dashboard_user_widget.blade.php: The widget's view

  • Override the Home Page view

First of all, we'll override the Home Page for displaying the widget:

Create a file located at resources/views/uccello/modules/home/index/main.blade.php to override the view.

We pass the current domain object to the widget using the variable $domain available in all pages of Uccello.

  • Configure the Widget's controller

Add the following code to the file located at app/Widgets/DashboardUserWidget.php:

We can use $this->config['domain'] to get the domain object passed to the widget. We use the inDomain() scope for retrieving only the users created in the current domain. Then we pass $totalUsersCount and $domainUsersCount variables to the widget's view.

  • Configure the Widget's view

Add the following code to the file located at resources/views/widgets/dashboard_user_widget.blade.php:

Our new dashboard widget is ready an it's displaying the number of users! Thanks to the jquery-countTo library, the numbers of users are animated.

Summary View widget

Although Summary View widgets can be created and used in the same way as Home Page widgets, Uccello provides a way to link a widget to a module and automatically add it to the module's Summary View.

We will create a widget that will be referenced in the database and that can be easily used with any module. The widget will display a list of all domains in which the user has a role.

  • Make the widget

You can easily create new widgets. To do this launch the following command:

This command create 2 files:

  1. app/Widgets/UserRolesWidget.php: The widget's controller

  2. resources/views/widgets/user_roles_widget.blade.php: The widget's view

  • Configure the Widget's controller

Add the following code to the file located at app/Widgets/UserRolesWidget.php:

As you can see in the file located at vendor/uccello/uccello/resources/views/modules/default/detail/summary.blade.php, several variable are passed to the widget and are available in $this->config.

  • Configure the widget's view

Add the following code to the file located at resources/views/widgets/user_roles_widget.blade.php:

  • Add the translations

Create a file located at resources/lang/en/widgets.php and add it the following code:

  • Add the widget into the database

First of all we will create a migration to reference the widget in the database and link it with the User module.

Launch the following command to create the migration:

Add this code into the migration file freshly created:

When you attach a widget to a module, you can pass the following pivot values:

  • sequence: Order in which to display the widget in the Summary View

  • data: A json encoded object to pass to the widget's controller. These data can be retrieved using (object) $this->config['data'].

Launch the migration with the following command:

Our new widget is ready an it's displaying the roles of a user in the Summary View of the record!

Properties

Attribute

Type

Description

Default

label

string

Widget's label. To translate it, create a localization file located at resources/lang/{lang_code}/widgets.php and add your translations.

type

string

Type of widget. For the moment only the summary option is available by default. It allows to display automatically the widget into a Summary View of a module.

class

string

Path to the widget's controller generated by the command php artisan make:widget WidgetName.

data

array

Data to pass to the widget.

  • You can use label to override the way how the label is made. By default the full label generated is$this->languagePackage.'widgets.'.$this->label .

  • In case or you are developing an external package, you must use package to set the name of your external package in which the widget is located (e.g. acme/my-package).

    This attribute is only useful for making sharable modules, installable with composer.

null

Last updated

Was this helpful?