Geta / geta-optimizely-categories

An alternative to Optimizely's default category functionality, where categories are instead stored as localizable IContent.
5 stars 10 forks source link

Only one category is visible in the category selector widget when [Categories] attribute is used on the item in a list property #32

Open wpelczar opened 5 months ago

wpelczar commented 5 months ago

Issue description

Categories attribute is not working properly when the item that uses it is added in a list property (https://world.optimizely.com/blogs/Per-Magne-Skuseth/Dates/2015/11/trying-out-propertylistt/)

Multiple categories can be selected, but after closing and reopening an item, only one category is selected.

How to reproduce

Below is the example that uses Alloy site.

  1. Install Geta.Optimizely.Categories nuget package (version 1.1.1) and define some categories
    
    [ContentType]
    public class BasicCategory : CategoryData
    {
    }

[ContentType] public class ExtendedCategory : BasicCategory { [CultureSpecific] public virtual XhtmlString MainBody { get; set; } }


2. Modify `StadardPage` to include property of type `IList<T>` where `T` is a new class with a list of categories as a property (in my example `ItemWithCategories`:

[SiteContentType(GUID = "9CCC8A41-5C8C-4BE0-8E73-520FF3DE8267")] [SiteImageUrl(Globals.StaticGraphicsFolderPath + "page-type-thumbnail-standard.png")] public class StandardPage : SitePageData { [Display( GroupName = SystemTabNames.Content, Order = 310)] [CultureSpecific] public virtual XhtmlString MainBody { get; set; }

[Display(
    GroupName = SystemTabNames.Content,
    Order = 320)]
public virtual ContentArea MainContentArea { get; set; }

[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<ItemWithCategories>))]
public virtual IList<ItemWithCategories> ItemsWithCategories { get; set; }

}

public class ItemWithCategories { [Display( Name = "Name", GroupName = SystemTabNames.Content, Order = 10)] public string Name { get; set; }

[Display(
    Name = "Test categories",
    GroupName = SystemTabNames.Content,
    Order = 20)]
[AllowedTypes(typeof(ExtendedCategory))]
[Categories]
public virtual IList<ContentReference> Categories { get; set; }

}

[PropertyDefinitionTypePlugIn] public class ItemWithCategoriesListProperty : PropertyList { }

3. Add page of that type in CMS and add item to the list selecting multiple categories

![image](https://github.com/Geta/geta-optimizely-categories/assets/9962438/dca7ad1d-3fc7-4524-a7c1-8fdc79304622)

![image](https://github.com/Geta/geta-optimizely-categories/assets/9962438/c45be64d-8ebc-43cd-98d9-96da234964e8)

4. Ids are correctly saved and show in the item list

![image](https://github.com/Geta/geta-optimizely-categories/assets/9962438/d8db0664-1d27-4af9-a03e-fb142adc0709)

5. But when I open that item again only one category is selected

![image](https://github.com/Geta/geta-optimizely-categories/assets/9962438/8c20bdfa-1b3c-4a34-ad9a-a66acdf931da)

6. If I click `OK` button, it will override saved category
![image](https://github.com/Geta/geta-optimizely-categories/assets/9962438/686f76c4-3093-4148-8d4b-a2ef6a832806)

### Expected behavior
When list item is edited it should display all categories.

### Notes
Similar situation is happening when  list property for inline block is used: https://world.optimizely.com/blogs/bartosz-sekula/dates/2023/1/official-list-property-support/

### Possible solution
I've found that code in `EPiServer/Shell/12.25.1/ClientResources/epi/shell/widgets.js` for `epi/patch/dijit/form/_FormMixin` inovke `set` method on descendant widgets, but it checks `multiple` flag. If it is set to `true` it passes the whole array, but if not, then, in this case, it only passes the first element from the array.

Adding a line like this: 

additionalValue.EditorConfiguration["multiple"] = (object)true;



In the `CreateDicplayMetadata` method in `CategoriesAttribute` solves this issue and all categories are properly shown.