Skip to content
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

Unable to create custom content without implementing ICategorizedContent #1649

Closed
marcelvwe opened this issue Jun 23, 2021 · 1 comment
Closed

Comments

@marcelvwe
Copy link

When creating a custom content class without implementing the ICategorizedContent interface, you won't be able to save the content in the management area, because the error '' is given.

I think this is caused by the SaveAsync(ContentEditModel model) method in \core\Piranha.Manager\Services\ContentService.cs. This method retrieves the content as DynamicContent, which implements the ICategorizedContent interface:

var content = await _api.Content.GetByIdAsync(model.Id, model.LanguageId);

if (content == null)
{
    content = await _factory.CreateAsync<DynamicContent>(contentType);
    content.Id = model.Id;
}

But, when saving, the ContentEditModel has the UseCategory property set as false (expected) since the original content type does not implement the interface.

When actually saving the content in \core\Piranha\Services\ContentService.cs method SaveAsync<T>(T model, Guid? languageId = null) where T : GenericContent, an check on the ICategorizedContent interface is performed, requiring the category to be specified. Since the DynamicContent class implements this interface, it is required to have the category set:

// Ensure category
if (model is ICategorizedContent categorizedModel)
{
    if (categorizedModel.Category == null || (string.IsNullOrWhiteSpace(categorizedModel.Category.Title) && string.IsNullOrWhiteSpace(categorizedModel.Category.Slug)))
    {
        throw new ValidationException("The Category field is required");
    }
}

I do not know the reasoning behind this check, why it has been added (and if it has a real purpose, why not performing the same checks on ITaggedContent for instance), or if it can be removed safely.

@tidyui
Copy link
Member

tidyui commented Jun 24, 2021

This is a bug. It will be fixed and released in a service release for 9.1. The condition is correct for typed models, but like you say, should be handled differently for dynamic models.

@tidyui tidyui added this to the Version 9.1 SR1 milestone Jun 24, 2021
@tidyui tidyui closed this as completed in 97cb199 Jun 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants