Skip to content

Commit

Permalink
feature #6090 CollectionField allow html (tpatartmajeur)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

CollectionField allow html

Allow to render HTML in options of association field including remote data

Commits
-------

ab68022 -
e7ca390 CollectionField allow html
  • Loading branch information
javiereguiluz committed Jul 12, 2024
2 parents 8a98b1e + ab68022 commit d92ca29
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 7 deletions.
5 changes: 3 additions & 2 deletions assets/js/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default class Autocomplete
}

#createAutocompleteWithRemoteData(element, autocompleteEndpointUrl) {
const renderOptionsAsHtml = 'true' === element.getAttribute('data-ea-autocomplete-render-items-as-html');
const config = this.#mergeObjects(this.#getCommonConfig(element), {
valueField: 'entityId',
labelField: 'entityAsString',
Expand Down Expand Up @@ -122,10 +123,10 @@ export default class Autocomplete
},
render: {
option: function(item, escape) {
return `<div>${escape(item.entityAsString)}</div>`;
return `<div>${renderOptionsAsHtml ? item.entityAsString : escape(item.entityAsString)}</div>`;
},
item: function(item, escape) {
return `<div>${escape(item.entityAsString)}</div>`;
return `<div>${renderOptionsAsHtml ? item.entityAsString : escape(item.entityAsString)}</div>`;
},
loading_more: function(data, escape) {
return `<div class="loading-more-results">${element.getAttribute('data-ea-i18n-loading-more-results')}</div>`;
Expand Down
11 changes: 10 additions & 1 deletion src/Field/AssociationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class AssociationField implements FieldInterface
public const OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME = 'crudEditPageName';
// the name of the property in the associated entity used to sort the results (only for *-To-One associations)
public const OPTION_SORT_PROPERTY = 'sortProperty';
public const OPTION_ESCAPE_HTML_CONTENTS = 'escapeHtml';

/**
* @param TranslatableInterface|string|false|null $label
Expand All @@ -58,7 +59,8 @@ public static function new(string $propertyName, $label = null): self
->setCustomOption(self::OPTION_DOCTRINE_ASSOCIATION_TYPE, null)
->setCustomOption(self::OPTION_RENDER_AS_EMBEDDED_FORM, false)
->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_NEW_PAGE_NAME, null)
->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME, null);
->setCustomOption(self::OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME, null)
->setCustomOption(self::OPTION_ESCAPE_HTML_CONTENTS, true);
}

public function autocomplete(): self
Expand Down Expand Up @@ -105,4 +107,11 @@ public function setSortProperty(string $orderProperty): self

return $this;
}

public function escapeHtml(bool $escape = true): self
{
$this->setCustomOption(self::OPTION_ESCAPE_HTML_CONTENTS, $escape);

return $this;
}
}
2 changes: 2 additions & 0 deletions src/Field/Configurator/AssociationConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
$field->setFormTypeOption('attr.data-ea-widget', 'ea-autocomplete');
}

$field->setFormTypeOption('attr.data-ea-autocomplete-render-items-as-html', true === $field->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS) ? 'false' : 'true');

// check for embedded associations
$propertyNameParts = explode('.', $propertyName);
if (\count($propertyNameParts) > 1) {
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Resources/public/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"/app.19616e33.rtl.css"
],
"js": [
"/app.5a01f8c5.js"
"/app.18be9a54.js"
]
},
"form": {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"app.css": "app.19616e33.css",
"app.rtl.css": "app.19616e33.rtl.css",
"app.js": "app.5a01f8c5.js",
"app.js": "app.18be9a54.js",
"form.js": "form.6e84c31d.js",
"page-layout.js": "page-layout.3347892e.js",
"page-color-scheme.js": "page-color-scheme.4c28406c.js",
Expand Down

0 comments on commit d92ca29

Please sign in to comment.