-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#114 - this is a first attempt at getting the text or rich text value… (
#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
Showing
7 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...Umbraco.MetaFields.Core/Common/Converters/SeoValueConverters/BaseGridSeoValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...mbraco.MetaFields.Core/Common/Converters/SeoValueConverters/BlockGridSeoValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...mbraco.MetaFields.Core/Common/Converters/SeoValueConverters/BlockListSeoValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters