Closed patrickdemooij9 closed 3 years ago
Made a little code that should do the trick (haven't tested it yet)
public IEnumerable<UmbracoEntityReference> GetReferences(object value)
{
var result = new List<UmbracoEntityReference>();
var json = value?.ToString();
var modelValue = _deserializer.Deserialize(json);
if (modelValue is null)
return result;
if (modelValue.Header != null)
{
result.AddRange(GetReferencesByBlock(modelValue.Header));
}
if (modelValue.Blocks?.Any() is true)
{
foreach (var block in modelValue.Blocks)
{
result.AddRange(GetReferencesByBlock(block));
}
}
IEnumerable<UmbracoEntityReference> GetReferencesByBlock(ContentBlockModelValue model)
{
if (_utils.GetDataType(model.DefinitionId) is IDataType dataType && dataType.Editor?.GetValueEditor() is IDataValueReference valueEditor)
{
return valueEditor.GetReferences(model.Content?.ToString());
}
return Enumerable.Empty<UmbracoEntityReference>();
}
return result;
}
EDIT: Just overwrote some of the content block classes on my end and it seems to be working correctly. Images in the header and/or content are now correctly generating references
Hi @patrickdemooij9, thanks for raising this issue we were not aware of this yet. Your code looks good, we should indeed just use the NestedContent implementation for this similar to FromEditor/ToEditor. Not sure when I have time to implement and check this but will try within the next week or 2. PR is always welcome to speed things up!
Hi @PerplexDaniel,
Just checked a bit more in the code as I was making a PR for the change. Unfortunately the IDataValueReference is an interface that has been added in 8.6.0. I am not quite sure what your stance is on updating Umbraco in the package to 8.6.0
@patrickdemooij9 Ah of course this references feature was added later. Anyway I don't mind if we raise the supported minimum Umbraco version to 8.6.0 for the next version, so you can include that in the PR. The package is mostly feature complete at the moment (v1.8) and if users really want some nifty new feature they will simply have to upgrade to the latest Umbraco first.
@PerplexDaniel, created a PR for it: https://github.com/PerplexDigital/Perplex.ContentBlocks/pull/54
@patrickdemooij9 Great! I'll have a look at it either this week or next week and will release it in a new version if it works correctly.
Whenever you put an image into a component in the content blocks, it'll not generate a relation in the database. This also means that it will not show up in the references tab: https://share.vidyard.com/watch/z8z8hft7yiJXAYv2DBkUq4?
The way to fix this is to have the ValueEditor implement IDataValueReference. This means you'll have to implement the method to retrieve all references. However, you should be able to mostly take this from https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs#L253 as it's almost the same as NestedContent.