-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Element picker: Add allowed element types configuration #23026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
acbd2ae
edb2e1b
6e1cac4
fe2758b
40b0d5f
ffd2376
a4b4abc
88f2e6f
b292b40
b5ef4fa
60c015a
2e353b3
00fd2fe
e263f7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| using Umbraco.Cms.Core.IO; | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Umbraco.Cms.Core.IO; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Models.Editors; | ||
| using Umbraco.Cms.Core.Models.Validation; | ||
| using Umbraco.Cms.Core.PropertyEditors.Validation; | ||
| using Umbraco.Cms.Core.Scoping; | ||
| using Umbraco.Cms.Core.Serialization; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Strings; | ||
| using Umbraco.Extensions; | ||
|
|
||
| namespace Umbraco.Cms.Core.PropertyEditors; | ||
|
|
||
|
|
@@ -39,9 +45,17 @@ | |
| IShortStringHelper shortStringHelper, | ||
| IJsonSerializer jsonSerializer, | ||
| IIOHelper ioHelper, | ||
| DataEditorAttribute attribute) | ||
| DataEditorAttribute attribute, | ||
| ILocalizedTextService localizedTextService, | ||
| IElementService elementService, | ||
| ICoreScopeProvider coreScopeProvider) | ||
| : base(shortStringHelper, jsonSerializer, ioHelper, attribute) | ||
| => _jsonSerializer = jsonSerializer; | ||
| { | ||
| _jsonSerializer = jsonSerializer; | ||
| Validators.Add(new ElementPickerValidatorRunner( | ||
| jsonSerializer, | ||
| new AllowedTypeValidator(localizedTextService, elementService, coreScopeProvider))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The media picker equivalent also has
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, Element Picker doesn't have min/max configurations. Is it possible to add this option to Element Picker?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think they are worth adding. In general we should look to align the document (content), media and element pickers. |
||
| } | ||
|
Check warning on line 58 in src/Umbraco.Core/PropertyEditors/ElementPickerPropertyEditor.cs
|
||
|
|
||
| public IEnumerable<UmbracoEntityReference> GetReferences(object? value) | ||
| { | ||
|
|
@@ -63,4 +77,105 @@ | |
| } | ||
| } | ||
| } | ||
|
|
||
| internal sealed class ElementPickerValidatorRunner : IValueValidator | ||
|
NguyenThuyLan marked this conversation as resolved.
Outdated
|
||
| { | ||
| private readonly AllowedTypeValidator _validator; | ||
|
|
||
| public ElementPickerValidatorRunner(IJsonSerializer jsonSerializer, AllowedTypeValidator validator) | ||
|
NguyenThuyLan marked this conversation as resolved.
Outdated
|
||
| => _validator = validator; | ||
|
|
||
| public IEnumerable<ValidationResult> Validate( | ||
| object? value, | ||
| string? valueType, | ||
| object? dataTypeConfiguration, | ||
| PropertyValidationContext validationContext) | ||
| { | ||
| if (dataTypeConfiguration is not ElementPickerConfiguration configuration) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| Guid[]? guids = value is IEnumerable<string> strings | ||
| ? strings.Select(s => Guid.TryParse(s, out Guid g) ? g : (Guid?)null).Where(g => g.HasValue).Select(g => g!.Value).ToArray() | ||
| : null; | ||
|
|
||
| return _validator.Validate(guids, configuration, valueType, validationContext); | ||
| } | ||
| } | ||
|
|
||
| internal sealed class AllowedTypeValidator : ITypedJsonValidator<Guid[], ElementPickerConfiguration> | ||
| { | ||
| private readonly ILocalizedTextService _localizedTextService; | ||
| private readonly IElementService _elementService; | ||
| private readonly ICoreScopeProvider _coreScopeProvider; | ||
|
|
||
| public AllowedTypeValidator( | ||
| ILocalizedTextService localizedTextService, | ||
| IElementService elementService, | ||
| ICoreScopeProvider coreScopeProvider) | ||
| { | ||
| _localizedTextService = localizedTextService; | ||
| _elementService = elementService; | ||
| _coreScopeProvider = coreScopeProvider; | ||
| } | ||
|
|
||
| public IEnumerable<ValidationResult> Validate( | ||
| Guid[]? value, | ||
| ElementPickerConfiguration? configuration, | ||
| string? valueType, | ||
| PropertyValidationContext validationContext) | ||
| { | ||
| if (value is null || value.Length == 0 || configuration is null) | ||
|
Check warning on line 129 in src/Umbraco.Core/PropertyEditors/ElementPickerPropertyEditor.cs
|
||
| { | ||
| return []; | ||
| } | ||
|
|
||
| HashSet<Guid> allowedContentTypeKeys = ParseAllowedContentTypeKeys(configuration.AllowedContentTypeIds); | ||
|
|
||
| // No filter configured — all element types are allowed. | ||
| if (allowedContentTypeKeys.Count == 0) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); | ||
| IElement[] elements = _elementService.GetByIds(value).ToArray(); | ||
|
NguyenThuyLan marked this conversation as resolved.
|
||
| scope.Complete(); | ||
|
|
||
| foreach (IElement element in elements) | ||
| { | ||
| if (allowedContentTypeKeys.Contains(element.ContentType.Key) is false) | ||
| { | ||
| return | ||
| [ | ||
| new ValidationResult( | ||
| _localizedTextService.Localize("validation", "invalidObjectType"), | ||
| ["value"]) | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| private static HashSet<Guid> ParseAllowedContentTypeKeys(string? configValue) | ||
| { | ||
| if (configValue.IsNullOrWhiteSpace()) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| var result = new HashSet<Guid>(); | ||
| foreach (var entry in configValue.Split(Constants.CharArrays.Comma, StringSplitOptions.RemoveEmptyEntries)) | ||
| { | ||
| if (Guid.TryParse(entry, out Guid guid)) | ||
| { | ||
| result.Add(guid); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { ManifestPropertyEditorUi } from '@umbraco-cms/backoffice/property-editor'; | ||
|
|
||
| export const manifest: ManifestPropertyEditorUi = { | ||
| type: 'propertyEditorUi', | ||
| alias: 'Umb.PropertyEditorUi.ElementPicker.AllowedElementTypes', | ||
| name: 'Element Picker Allowed Element Types Property Editor UI', | ||
| element: () => | ||
| import('./property-editor-ui-element-picker-allowed-element-types.element.js'), | ||
| meta: { | ||
| label: 'Element Picker Allowed Element Types', | ||
| icon: 'icon-plugin', | ||
| group: '#propertyEditorUIGroups_pickers', | ||
| }, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { customElement, html, property } from '@umbraco-cms/backoffice/external/lit'; | ||
| import { UmbChangeEvent } from '@umbraco-cms/backoffice/event'; | ||
| import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; | ||
| import type { UmbPropertyEditorUiElement } from '@umbraco-cms/backoffice/property-editor'; | ||
|
|
||
| import '@umbraco-cms/backoffice/document-type'; | ||
|
|
||
| @customElement('umb-property-editor-ui-element-picker-allowed-element-types') | ||
| export class UmbPropertyEditorUIElementPickerAllowedElementTypesElement | ||
| extends UmbLitElement | ||
| implements UmbPropertyEditorUiElement | ||
| { | ||
| @property() | ||
| public set value(value: string) { | ||
| this.#selection = value ? value.split(',') : []; | ||
| } | ||
| public get value(): string { | ||
| return this.#selection.join(','); | ||
| } | ||
|
|
||
| #selection: Array<string> = []; | ||
|
|
||
| #onChange(event: CustomEvent & { target: { selection: string[] } }) { | ||
| this.value = event.target.selection.join(','); | ||
| this.dispatchEvent(new UmbChangeEvent()); | ||
| } | ||
|
|
||
| override render() { | ||
| return html`<umb-input-document-type | ||
| .elementTypesOnly=${true} | ||
| .selection=${this.#selection} | ||
| @change=${this.#onChange}></umb-input-document-type>`; | ||
| } | ||
| } | ||
|
|
||
| export default UmbPropertyEditorUIElementPickerAllowedElementTypesElement; | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| 'umb-property-editor-ui-element-picker-allowed-element-types': UmbPropertyEditorUIElementPickerAllowedElementTypesElement; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.