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 Dec 4, 2024
1 parent d726d56 commit d8ce7b9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 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 @@ -564,13 +564,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 @@ -503,6 +503,7 @@
]
],
'assets' => [
'tinymce',
'dashboard' => ['dashboard'],
'rack' => ['gridstack', 'rack'],
'printer' => ['dashboard'],
Expand Down Expand Up @@ -533,7 +534,7 @@
],
],
'config' => [
'assetdefinition' => ['sortable'],
'assetdefinition' => ['sortable', 'tinymce'],
'commondropdown' => [
'ITILFollowupTemplate' => ['tinymce'],
'ProjectTaskTemplate' => ['tinymce'],
Expand Down
3 changes: 3 additions & 0 deletions tests/cypress/e2e/Asset/custom_fields.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ 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]]));

cy.findByLabelText('Test RichText').awaitTinyMCE();

// 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 d8ce7b9

Please sign in to comment.