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:
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:
It is better to use translation for displaying "Total" and "Current domain" strings.
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:
phpartisanmake:widgetUserRolesWidget
This command create 2 files:
app/Widgets/UserRolesWidget.php: The widget's controller
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:
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:
phpartisanmake:migrationadd_user_roles_widget
Add this code into the migration file freshly created:
<?phpuseIlluminate\Database\Migrations\Migration;useUccello\Core\Models\Module;useUccello\Core\Models\Widget;classAddUserRolesWidgetextendsMigration{/** * Run the migrations. * * @returnvoid */publicfunctionup() {// Add the widget into the list of widgets $widget =Widget::create(['label'=>'widget.user_roles','type'=>'summary','class'=>'App\Widgets\UserRolesWidget','data'=>null ]);// Link the widget to the User module $module =Module::where('name','user')->first(); $module->widgets()->attach($widget->id, [ 'sequence'=>0]); }/** * Reverse the migrations. * * @returnvoid */publicfunctiondown() {Widget::where('label','widget.user_roles')->where('type','summary')->where('class','App\Widgets\UserRolesWidget')->delete(); }}
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:
phpartisanmigrate
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.