An uitype is a graphical element used to represent a form field and format its value according to a context.
Uccello allows you to create forms made up of fields of different types. Each field type corresponds to a uitype.
Each uitype is autonomous, independent and able to format data:
In a database
In a vue
In table cells
In the response to an API call
Similarly, each uitype is able to generate a form field to display on an edit page.
Flexible
Thanks to the high flexibility of uccello, it is possible to create simplistic uitypes (a simple value) as well as complex uitypes (display using external libraries).
For example, the text uitype only displays plain text while the entity uitype is able to link one data to another that may be located in a different module. The date uitype allows you to select a date from a calendar.
Configurable
The configuration of a uitype is done at several levels.
migration file: Type of the column in the database and type of field used in the form
php class: Formatting the value when saving and displaying
blade files : Displaying the form field and defining a layout in a view
Let's take the example of the text uitype:
Migration file
// Creation of the tableSchema::create('people',function (Blueprint $table) { $table->increments('id'); $table->string('name'); // This column will be link to a text uitype $table->timestamps();});...// Creation of the field$field =Field::create(['name'=>'name','uitype_id'=>uitype('text')->id,// Use of the text uitype'displaytype_id'=>displaytype('everywhere')->id,'sequence'=>0,'block_id'=> $block->id,'module_id'=> $module->id]);
$table->string('name') : Creta a column namedname with type VARCHAR(255)
'uitype_id' => uitype('text')->id : The field uses the text uitype
Php class
The text uitype is defined in the file app/Fields/Uitype/Text.php. Here is a summary after integration of the traits used.
namespaceUccello\Core\Fields\Uitype;useUccello\Core\Contracts\Field\Uitype;useUccello\Core\Fields\Traits\DefaultUitype;useUccello\Core\Fields\Traits\UccelloUitype;useUccello\Core\Models\Field;useUccello\Core\Models\Module;useUccello\Core\Models\Domain;classTextimplementsUitype{/** * Name of the package in which the uitype is located * Useful for Blade to find the associated template files */public $package ='uccello';/** * Field type used by Form builder */publicfunctiongetFormType():string {return'text'; }/** * Field options used by Form Builder */publicfunctiongetFormOptions($record,Field $field,Module $module):array {return []; }/** * Default database column name */publicfunctiongetDefaultDatabaseColumn(Field $field) :string {return $field->name; }/** * Default icon to use in the form field */publicfunctiongetDefaultIcon() :?string {returnnull; }/** * Formatting the value for a display in a view or in an API response */publicfunctiongetFormattedValueToDisplay(Field $field, $record) :string {return $record->{$field->column} ??''; }/** * Formatting the value before saving it in the database */ public function getFormattedValueToSave(Request $request, Field $field, $value, $record = null, ?Domain $domain = null, ?Module $module = null) : ?string
{return $value; }/** * Returns formatted value to save with config. * * @param\Illuminate\Http\Request $request * @param\Uccello\Core\Models\Field $field * @parammixed|null $value * @parammixed|null $record * @param\Uccello\Core\Models\Domain|null $domain * @param\Uccello\Core\Models\Module|null $module * @returnstring|null */ public function getFormattedValueToSaveWithConfig(Request $request, Field $field, $value, $config, $record = null, ?Domain $domain = null, ?Module $module = null) : ?string
{return$this->getFormattedValueToSave($request, $field, $value, $record, $domain, $module); }/** * Formatting the value before searching the database. * By default adds % at the beginning end the ending to make a 'like' query. */publicfunctiongetFormattedValueToSearch($value) { $formattedValue = $value;if ($formattedValue) { $formattedValue ="%$value%"; }return $formattedValue; }/** * Adding the condition in the SQL query */publicfunctionaddConditionToSearchQuery(Builder $query,Field $field, $value) :Builder {if (!$this->isEmptyOrNotEmptySearchQuery($query, $field, $value)) { $formattedValue =$this->getFormattedValueToSearch($value); $query = $query->where($field->column,'like', $formattedValue); }return $query; }/** * Returns true if the search value is 'Null' or 'NotNull' */protectedfunctionisEmptyOrNotEmptySearchQuery(Builder&$query,Field $field, $value):bool {if ($value ===uctrans('filter.search_flag.empty', $field->module)) { $query->where(function ($q) use ($field) { $q->whereNull($field->column)->orWhere($field->column,'=',''); });returntrue; } elseif ($value ===uctrans('filter.search_flag.not_empty', $field->module)) { $query = $query->whereNotNull($field->column)->where($field->column,'!=','');returntrue; }returnfalse; }/** * Ask the user some specific options relative to a field. * Useful with Module Designer. */ public function askFieldOptions(\StdClass &$module, \StdClass &$field, InputInterface $input, OutputInterface $output)
{ $repeated = $output->confirm('Would you like to repeat this field (for confirmation)?',false);if ($repeated) { $field->data->repeated =true; } }/** * Way to create the column into the database. * Useful for Module Designer. */publicfunctioncreateFieldColumn(Field $field,Blueprint $table) :Fluent {return $table->string($this->getDefaultDatabaseColumn($field)); }/** * Text to add into the module migration.* * Useful for Module Designer. */publicfunctioncreateFieldColumnStr(Field $field) :string { $column =$this->getDefaultDatabaseColumn($field);return"\$table->string('$column')"; }}
Blade files
There are at least four blade files per uitype. One for the layout of the form field in the edit view, one for displaying the field value in the detail view, one for searching from the list view and one for displaying the field value in the list view.