I’m using SEO Toolkit (thanks @patrickdemooij9 !) and am exploring fallback values. I’d like to get the value from a node that I’ve selected through a content picker as the fallback value. For example, if I select a product in the content picker and I don’t add a meta title for the page, it should fallback to the content picker Product.CustomTitle property.
I took a snoop through the source code and came to the conclusion that this wasn’t possible out of the box, but wondered if I’m either wrong, or someone else has tried to do something similar.
After some digging around in the source code I managed to get this working using the FieldProvider mechanism that SEO Toolkit is based on. The general premise is that these appear in the fallback selection in the back office so can apply to any of the meta fields added. This allows the functionality of SEO Toolkit to remain, and if someone adds meta directly on the page it shows that over the fallback.
The actual selection of content could be improved, but the general premise is here:
using SeoToolkit.Umbraco.MetaFields.Core.Common.FieldProviders;
using SeoToolkit.Umbraco.MetaFields.Core.Models.Converters;
public class CustomFieldProvider : IFieldProvider
{
public IEnumerable<FieldItemViewModel> GetFieldItems()
{
return new[]
{
new FieldItemViewModel("Selected Content Property", "custom-selectedContentProperty")
};
}
public object HandleFieldItem(FieldsItem fieldsItem, IPublishedContent content, string fieldAlias)
{
// MyContentModel is the models builder output of my content model
MyContentModel stContent = content as MyContentModel;
if (stContent != null)
{
switch(fieldAlias)
{
case "title":
// SelectedContent is the content picker, linking off to another node
return stContent.SelectedContent?.Name;
break;
}
}
return null;
}
}
And if we register it in our composer it then allows us to select it in the back office:
using SeoToolkit.Umbraco.MetaFields.Core.Collections;
public class SeoToolkitComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.WithCollectionBuilder<FieldProviderCollectionBuilder>()
.Add<CustomFieldProvider>();
}
}