Skip to content

Commit

Permalink
add richtext custom field type
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Jan 8, 2025
1 parent 69d2c6f commit 272bbf9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
10 changes: 9 additions & 1 deletion js/src/vue/CustomObject/FieldPreview/FieldDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@
sortable_fields.delete(key);
}
function getPreviewDisplayStyle(preview_html) {
const preview = $(`<div>${preview_html}</div>`);
if (preview.find('.fileupload').length > 0) {
return 'block';
}
return 'contents';
}
onMounted(() => {
//for each field in fields_display, add it to the list using the template and slot
appendField(fields_display.map((field) => field.key));
Expand Down Expand Up @@ -270,7 +278,7 @@
</template>
</template>
<template v-slot:field_preview v-if="sortable_field.preview_html">
<div v-html="sortable_field.preview_html" style="display: contents"></div>
<div v-html="sortable_field.preview_html" :style="`display: ${getPreviewDisplayStyle(sortable_field.preview_html)}`"></div>
</template>
</Field>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/Glpi/Asset/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Dropdown;
use Entity;
use Glpi\Application\View\TemplateRenderer;
use Glpi\Asset\Capacity\HasDocumentsCapacity;
use Glpi\CustomObject\CustomObjectTrait;
use Group;
use Group_Item;
Expand Down
6 changes: 3 additions & 3 deletions src/Glpi/Asset/AssetDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,13 @@ public function getAssetTypeDictionaryCollectionClassName(bool $with_namespace =
/**
* Indicates whether the given capacity is enabled.
*
* @param CapacityInterface $capacity
* @param CapacityInterface|class-string<CapacityInterface> $capacity
* @return bool
*/
public function hasCapacityEnabled(CapacityInterface $capacity): bool
public function hasCapacityEnabled(CapacityInterface|string $capacity): bool
{
$enabled_capacities = $this->getDecodedCapacitiesField();
return in_array($capacity::class, $enabled_capacities);
return in_array(is_string($capacity) ? $capacity : $capacity::class, $enabled_capacities);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Glpi/Asset/CustomFieldType/TextType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
namespace Glpi\Asset\CustomFieldType;

use Glpi\Application\View\TemplateRenderer;
use Glpi\Asset\CustomFieldOption\BooleanOption;

class TextType extends AbstractType
{
Expand All @@ -43,6 +44,14 @@ public static function getName(): string
return __('Text');
}

public function getOptions(): array
{
$opts = parent::getOptions();
$opts[] = new BooleanOption($this->custom_field, 'enable_richtext', __('Rich text'), true, false);
$opts[] = new BooleanOption($this->custom_field, 'enable_images', __('Allow images'), true, false);
return $opts;
}

public function getFormInput(string $name, mixed $value, ?string $label = null, bool $for_default = false): string
{
$twig_params = [
Expand Down
3 changes: 2 additions & 1 deletion src/autoload/CFG_GLPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@
]
],
'assets' => [
'tinymce',
'dashboard' => ['dashboard'],
'rack' => ['gridstack', 'rack'],
'printer' => ['dashboard'],
Expand Down Expand Up @@ -541,7 +542,7 @@
],
],
'config' => [
'assetdefinition' => ['sortable'],
'glpi\asset\assetdefinition' => ['sortable', 'tinymce'],
'commondropdown' => [
'ITILFollowupTemplate' => ['tinymce'],
'ProjectTaskTemplate' => ['tinymce'],
Expand Down
5 changes: 2 additions & 3 deletions templates/pages/admin/customobjects/main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@
}
const reserved_names = {{ reserved_system_names|json_encode()|raw }}.map((n) => n.toLowerCase());
const existing_names = {{ existing_system_names|json_encode()|raw }}.map((n) => n.toLowerCase());
$('#mainformtable input[name="system_name"]').val(
$('#mainformtable input[name="label"]').val().normalize('NFD').replace(/[^a-z]/gi, '')
);
$('#mainformtable input[name="system_name"]')
.val($('#mainformtable input[name="label"]').val().normalize('NFD').replace(/[^a-z]/gi, ''));
const system_name = $('#mainformtable input[name="system_name"]').val().toLowerCase();
if (reserved_names.includes(system_name) || system_name.endsWith('type') || system_name.endsWith('model')) {
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity(__('The system name is a reserved name. Please enter a different label or manually change the system name.'));
Expand Down
7 changes: 7 additions & 0 deletions tests/cypress/e2e/Asset/custom_fields.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ describe("Custom Assets - Custom Fields", () => {
if (options.has('mandatory')) {
cy.findByLabelText('Mandatory').check();
}
if (options.has('enable_richtext')) {
cy.findByLabelText('Rich text').check();
}
if (options.has('enable_images')) {
cy.findByLabelText('Allow images').check();
}

cy.findByRole('button', {name: 'Add'}).click();
cy.waitForNetworkIdle('/front/asset/customfielddefinition.form.php', 100);
Expand Down Expand Up @@ -197,6 +203,7 @@ describe("Custom Assets - Custom Fields", () => {
createField('Test MultiDropdown', 'Dropdown', new Map([['item_type', 'Monitor'], ['multiple_values', true]]));
createField('Test URL', 'URL');
createField('Test YesNo', 'Yes/No');
createField('Test RichText', 'Text', new Map([['enable_richtext', true], ['enable_images', true]]));

// Intercept form submission to check the form display values sent
cy.intercept('POST', '/front/asset/assetdefinition.form.php').as('saveFieldsDisplay');
Expand Down

0 comments on commit 272bbf9

Please sign in to comment.