Before learning any modifications, visit the pages below to understand the form builder’s structure.
P.S: The display_for_user and required_for_user attributes can be ignored since user roles can’t be accessed SD modules.
Adding a New Asset Type
To add asset type, use the following facade method:
$asset_type = \App\Plugins\ServiceDesk\Model\Assets\SdAssettypes::updateOrCreate(['key' => 'test']['name' => 'Test']);
App\Form\Facades\FormCreator::addOrUpdateSections($asset_type, $form_sections);
For more information, check the file located at app/Plugins/ServiceDesk/database/seeds/v_9_0_1_RC_1/NewFormSeeder.php
, under the method assetTypes()
.
Typically, when adding a new default asset type with sections, it will only contain the custom form fields.
Adding a New Section to an Existing Asset type
To add a new section to an existing asset type, use the following steps:
- Find the form category where you want to add the new section by fetching it through its category ID:
$asset_type = \App\Plugins\ServiceDesk\Model\Assets\SdAssettypes::find(pass the category id where you have to add the new section);
- Define the new fields under the
form_fields
key, following the section format. - Use the facade method to update the sections in the existing asset type:
App\Form\Facades\FormCreator::addOrUpdateSections($asset_type, $form_sections);
Adding a New Form Field to an Existing Asset Type
- First, find the asset type where you want to add the new field:
$asset_type = \App\Plugins\ServiceDesk\Model\Assets\SdAssettypes::find(pass the category id where you have to add the new section);
- Then, search for the specific section in which you want to add the field. Here’s an example query to get the first section of the existing form:
$existing_section = App\Form\Models\FormSection::where('category_type', $asset_type::class)->where('category_id', $asset_type->id)->first();
You can modify this query if you want to add the field to a different section.
- Now, you need to add two mandatory attributes for every form field:
category_identifier
. These values correspond to the existing asset type form category. A sample form field structure looks like this:
$form_fields = [[
'key' => '',
'title' => '',
'data_type' => '',
'api_info' => '',
'sort_order' => 1,
'required_for_agent' => 1,
'display_for_agent' => 1,
'is_deletable' => 0,
'labels' => [[
'label' => '',
'language' => 'en'
]],
'default' => 1,
'is_observable' => 0,
'is_filterable' => 1,
'is_configurable' => 1,
'is_user_config' => 1,
'is_agent_config' => 1,
'column' => 'first',
'is_active' => 1,
'is_edit_visible' => 1,
'category_identifier' => 'asset_type'
]];
- Finally, use this method to save the new form field to the existing section:
App\Form\Facades\FormCreator::saveFormFields($existing_section, $form_fields, 'asset_type');
Handling Nested Fields in Custom Form Fields
If the custom field supports adding nested fields (e.g., for select, checkbox, or radio types), you need to append an options
attribute to the form field structure. Here’s an example format for the options
attribute:
$options = [[
'value' => 'one',
'labels' => [[
'label' => 'one',
'language' => 'en'
]],
],
[
'value' => 'two',
'labels' => [[
'label' => 'two',
'language' => 'en'
]],
'nodes' => [[
'key' => '',
'title' => '',
'data_type' => '',
'api_info' => '',
'sort_order' => 1,
'required_for_agent' => 1,
'required_for_user' => 1,
'display_for_agent' => 1,
'display_for_user' => 1,
'is_deletable' => 0,
'labels' => [[
'label' => '',
'language' => 'en'
]],
'default' => 1,
'is_observable' => 0,
'is_filterable' => 1,
'is_configurable' => 1,
'is_user_config' => 1,
'is_agent_config' => 1,
'column' => 'first',
'is_active' => 1,
'is_edit_visible' => 1,
'category_identifier' => 'asset_type'
]]
]];
After appending options to the form field structure, it will look like this:
$form_fields = [[
'key' => '',
'title' => '',
'data_type' => '',
'api_info' => '',
'sort_order' => 1,
'required_for_agent' => 1,
'required_for_user' => 1,
'display_for_agent' => 1,
'display_for_user' => 1,
'is_deletable' => 0,
'labels' => [[
'label' => '',
'language' => 'en'
]],
'default' => 1,
'is_observable' => 0,
'is_filterable' => 1,
'is_configurable' => 1,
'is_user_config' => 1,
'is_agent_config' => 1,
'column' => 'first',
'is_active' => 1,
'is_edit_visible' => 1,
'category_identifier' => 'asset_type',
'options' => $options
]];
Note: Nested fields are only supported for radio, select, and checkbox data types.
Explanation of Key Concepts:
- addOrUpdateSections: This method allows adding new sections to an existing asset form. You can define multiple fields in the
form_fields
key of the section format. - saveFormFields: This is the method responsible for adding new form fields to an existing section. It ensures that the fields are saved under the correct form category and section.
- Nested Fields: Fields like radio buttons, checkboxes, or select dropdowns can have nested fields, represented using the
options
attribute. Each option has a value, and label, and may also contain additional form fields (nodes) associated with it.
By following these steps, you can easily extend or modify the existing form structure within the product, ensuring seamless integration and accurate field management.