Skip to content

Commit

Permalink
#114 - this is a first attempt at getting the text or rich text value… (
Browse files Browse the repository at this point in the history
#235)

* #114 - this is a first attempt at getting the text or rich text values from a block list or grid for description meta fields

* Added checking for html encoded strings and stripping the html.
  • Loading branch information
robertjf authored Sep 23, 2023
1 parent ad07e56 commit 8707534
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Extensions;
using SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Converters;
using Umbraco.Cms.Core.Strings;

namespace SeoToolkit.Umbraco.MetaFields.Core.Common.Converters.SeoValueConverters
{
public abstract class BaseGridSeoValueConverter<T> : ISeoValueConverter
where T : class, IBlockReference<IPublishedElement, IPublishedElement>
{
public virtual Type FromValue {get;}
public virtual Type ToValue {get;}
public virtual object Convert(object value, IPublishedContent currentContent, string fieldAlias)
{
var dataTypes = new string[]{
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextArea,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextBox,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TinyMce
};
// Walk the Block List and get any text values in a list.
List<object> values = new();
if (value is BlockModelCollection<T> blocks) {
foreach(var item in blocks.Select(b => b.Content))
{
// Go through each of the properties in the content element that are Text, Text Area or Rich Text and aggregate their values into a single string.
foreach(var property in item.Properties.Where(p => dataTypes.Contains(p.PropertyType.DataType.EditorAlias))) {
var propertyValue = item.Value(property.Alias);
if (propertyValue is IHtmlEncodedString encodedString) {
values.Add(encodedString.ToHtmlString()?.StripHtml());
} else {
values.Add(propertyValue);
}

// TODO: If there is an embedded Block Grid or Block List item, recurse into it and get the values.
}
}
}
return values.Aggregate("", (a, b) => $"{a} {b}").Trim();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Umbraco.Cms.Core.Models.Blocks;

namespace SeoToolkit.Umbraco.MetaFields.Core.Common.Converters.SeoValueConverters
{
public class BlockGridSeoValueConverter : BaseGridSeoValueConverter<BlockGridItem>
{
public override Type FromValue => typeof(BlockGridModel);
public override Type ToValue => typeof(string);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Umbraco.Cms.Core.Models.Blocks;

namespace SeoToolkit.Umbraco.MetaFields.Core.Common.Converters.SeoValueConverters
{
public class BlockListSeoValueConverter : BaseGridSeoValueConverter<BlockListItem>
{
public override Type FromValue => typeof(BlockListModel);
public override Type ToValue => typeof(string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public void Compose(IUmbracoBuilder builder)
.Add<PublishedContentSeoValueConverter>()
.Add<FieldSeoValueConverter>()
.Add<MultiplePublishedContentSeoValueConverter>()
.Add<BlockGridSeoValueConverter>()
.Add<BlockListSeoValueConverter>()
.Add<HtmlEncodedStringSeoConverter>();

builder.WithCollectionBuilder<MapDefinitionCollectionBuilder>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public class OpenGraphDescriptionField : ISeoField
public string GroupAlias => SeoFieldGroupConstants.SocialMediaGroup;
public Type FieldType => typeof(string);

public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] { "Umbraco.TextBox", "Umbraco.TextArea", "Umbraco.TinyMCE" });
public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] {
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextArea,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextBox,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TinyMce,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockList,
});
public ISeoFieldEditEditor EditEditor => new SeoTextAreaEditEditor();

public HtmlString Render(object value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public class SeoDescriptionField : ISeoField
public string GroupAlias => SeoFieldGroupConstants.MetaFieldsGroup;
public Type FieldType => typeof(string);

public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] { "Umbraco.TextBox", "Umbraco.TextArea", "Umbraco.TinyMCE" });
public ISeoFieldEditor Editor => new SeoFieldFieldsEditor(new[] {
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextArea,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TextBox,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.TinyMce,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockGrid,
global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.BlockList,
});
public ISeoFieldEditEditor EditEditor => new SeoTextAreaEditEditor();

public HtmlString Render(object value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
vm.isContentDirty = function () {
var currentEditorItem = editorState.getCurrent();
var isDirty = false;
currentEditorItem.variants.forEach(function (variant) {
if (variant.isDirty) {
isDirty = true;
}
});
if (currentEditorItem.variants !== undefined) {
currentEditorItem.variants.forEach(function (variant) {
if (variant.isDirty) {
isDirty = true;
}
});
}
return isDirty;
}

Expand Down

0 comments on commit 8707534

Please sign in to comment.