Simplify creating content from a blueprint programmatically (#19528)
* Rename `IContentService.CreateContentFromBlueprint` to `CreateBlueprintFromContent` In reality, this method is used by the core to create a blueprint from content, and not the other way around, which doesn't need new ids. This was causing confusion, so the old name has been marked as deprecated in favor of the new name. If developers want to create content from blueprints they should use `IContentBlueprintEditingService.GetScaffoldedAsync()` instead, which is what is used by the management api. * Added integration tests to verify that new block ids are generated when creating content from a blueprint * Return copy of the blueprint in `ContentBlueprintEditingService.GetScaffoldedAsync` instead of the blueprint itself * Update CreateContentFromBlueprint xml docs to mention both replacement methods * Fix tests for rich text blocks * Small re-organization * Adjusted tests that were still referencing `ContentService.CreateContentFromBlueprint` * Add default implementation to new CreateBlueprintFromContent method * Update tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentBlueprintEditingServiceTests.GetScaffold.cs Co-authored-by: Andy Butland <abutland73@gmail.com> --------- Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.IO;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.PropertyEditors;
|
||||
using Umbraco.Cms.Infrastructure.Serialization;
|
||||
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
||||
using Umbraco.Cms.Tests.Common.Builders.Interfaces;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Common.Builders;
|
||||
@@ -155,4 +158,100 @@ public class DataTypeBuilder
|
||||
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public static DataType CreateSimpleElementDataType(
|
||||
IIOHelper ioHelper,
|
||||
string editorAlias,
|
||||
Guid elementKey,
|
||||
Guid? elementSettingKey)
|
||||
{
|
||||
Dictionary<string, object> configuration = editorAlias switch
|
||||
{
|
||||
Constants.PropertyEditors.Aliases.BlockGrid => GetBlockGridBaseConfiguration(),
|
||||
Constants.PropertyEditors.Aliases.RichText => GetRteBaseConfiguration(),
|
||||
_ => [],
|
||||
};
|
||||
|
||||
SetBlockConfiguration(
|
||||
configuration,
|
||||
elementKey,
|
||||
elementSettingKey,
|
||||
editorAlias == Constants.PropertyEditors.Aliases.BlockGrid ? true : null);
|
||||
|
||||
|
||||
var dataTypeBuilder = new DataTypeBuilder()
|
||||
.WithId(0)
|
||||
.WithDatabaseType(ValueStorageType.Nvarchar)
|
||||
.AddEditor()
|
||||
.WithAlias(editorAlias);
|
||||
|
||||
switch (editorAlias)
|
||||
{
|
||||
case Constants.PropertyEditors.Aliases.BlockGrid:
|
||||
dataTypeBuilder.WithConfigurationEditor(
|
||||
new BlockGridConfigurationEditor(ioHelper) { DefaultConfiguration = configuration });
|
||||
break;
|
||||
case Constants.PropertyEditors.Aliases.BlockList:
|
||||
dataTypeBuilder.WithConfigurationEditor(
|
||||
new BlockListConfigurationEditor(ioHelper) { DefaultConfiguration = configuration });
|
||||
break;
|
||||
case Constants.PropertyEditors.Aliases.RichText:
|
||||
dataTypeBuilder.WithConfigurationEditor(
|
||||
new RichTextConfigurationEditor(ioHelper) { DefaultConfiguration = configuration });
|
||||
break;
|
||||
}
|
||||
|
||||
return dataTypeBuilder.Done().Build();
|
||||
}
|
||||
|
||||
private static void SetBlockConfiguration(
|
||||
Dictionary<string, object> dictionary,
|
||||
Guid? elementKey,
|
||||
Guid? elementSettingKey,
|
||||
bool? allowAtRoot)
|
||||
{
|
||||
if (elementKey is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dictionary["blocks"] = new[] { BuildBlockConfiguration(elementKey.Value, elementSettingKey, allowAtRoot) };
|
||||
}
|
||||
|
||||
private static Dictionary<string, object> GetBlockGridBaseConfiguration() => new() { ["gridColumns"] = 12 };
|
||||
|
||||
private static Dictionary<string, object> GetRteBaseConfiguration()
|
||||
{
|
||||
var dictionary = new Dictionary<string, object>
|
||||
{
|
||||
["maxImageSize"] = 500,
|
||||
["mode"] = "Classic",
|
||||
["toolbar"] = new[]
|
||||
{
|
||||
"styles", "bold", "italic", "alignleft", "aligncenter", "alignright", "bullist", "numlist", "outdent",
|
||||
"indent", "sourcecode", "link", "umbmediapicker", "umbembeddialog"
|
||||
},
|
||||
};
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
private static Dictionary<string, object> BuildBlockConfiguration(
|
||||
Guid? elementKey,
|
||||
Guid? elementSettingKey,
|
||||
bool? allowAtRoot)
|
||||
{
|
||||
var dictionary = new Dictionary<string, object>();
|
||||
if (allowAtRoot is not null)
|
||||
{
|
||||
dictionary.Add("allowAtRoot", allowAtRoot.Value);
|
||||
}
|
||||
|
||||
dictionary.Add("contentElementTypeKey", elementKey.ToString());
|
||||
if (elementSettingKey is not null)
|
||||
{
|
||||
dictionary.Add("settingsElementTypeKey", elementSettingKey.ToString());
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user