public IEnumerable <UmbracoEntityReference> GetReferences(object?value)
            {
                var rawJson = value == null ? string.Empty : value is string str ? str : value.ToString();

                var result          = new List <UmbracoEntityReference>();
                var blockEditorData = _blockEditorValues.DeserializeAndClean(rawJson);

                if (blockEditorData == null)
                {
                    return(Enumerable.Empty <UmbracoEntityReference>());
                }

                // loop through all content and settings data
                foreach (var row in blockEditorData.BlockValue.ContentData.Concat(blockEditorData.BlockValue.SettingsData))
                {
                    foreach (var prop in row.PropertyValues)
                    {
                        var propEditor = _propertyEditors[prop.Value.PropertyType.PropertyEditorAlias];

                        var valueEditor = propEditor?.GetValueEditor();
                        if (!(valueEditor is IDataValueReference reference))
                        {
                            continue;
                        }

                        var val = prop.Value.Value?.ToString();

                        var refs = reference.GetReferences(val);

                        result.AddRange(refs);
                    }
                }

                return(result);
            }
Example #2
0
            public IEnumerable<ValidationResult> Validate(object value, string valueType, object dataTypeConfiguration)
            {
                var blockConfig = (BlockListConfiguration)dataTypeConfiguration;
                if (blockConfig == null) yield break;

                var validationLimit = blockConfig.ValidationLimit;
                if (validationLimit == null) yield break;

                var blockEditorData = _blockEditorValues.DeserializeAndClean(value);

                if ((blockEditorData == null && validationLimit.Min.HasValue && validationLimit.Min > 0)
                    || (blockEditorData != null && validationLimit.Min.HasValue && blockEditorData.Layout.Count() < validationLimit.Min))
                {
                    yield return new ValidationResult(
                        _textService.Localize("validation/entriesShort", new[]
                        {
                            validationLimit.Min.ToString(),
                            (validationLimit.Min - blockEditorData.Layout.Count()).ToString()
                        }),
                        new[] { "minCount" });
                }

                if (blockEditorData != null && validationLimit.Max.HasValue && blockEditorData.Layout.Count() > validationLimit.Max)
                {
                    yield return new ValidationResult(
                        _textService.Localize("validation/entriesExceed", new[]
                        {
                            validationLimit.Max.ToString(),
                            (blockEditorData.Layout.Count() - validationLimit.Max).ToString()
                        }),
                        new[] { "maxCount" });
                }
            }
Example #3
0
            protected override IEnumerable <ElementTypeValidationModel> GetElementTypeValidation(object value)
            {
                var blockEditorData = _blockEditorValues.DeserializeAndClean(value);

                if (blockEditorData != null)
                {
                    foreach (var row in blockEditorData.BlockValue.ContentData.Concat(blockEditorData.BlockValue.SettingsData))
                    {
                        var elementValidation = new ElementTypeValidationModel(row.ContentTypeAlias, row.Key);
                        foreach (var prop in row.PropertyValues)
                        {
                            elementValidation.AddPropertyTypeValidation(
                                new PropertyTypeValidationModel(prop.Value.PropertyType, prop.Value.Value));
                        }
                        yield return(elementValidation);
                    }
                }
            }
        protected override IEnumerable <ElementTypeValidationModel> GetElementTypeValidation(object?value)
        {
            BlockEditorData?blockEditorData = _blockEditorValues.DeserializeAndClean(value);

            if (blockEditorData != null)
            {
                // There is no guarantee that the client will post data for every property defined in the Element Type but we still
                // need to validate that data for each property especially for things like 'required' data to work.
                // Lookup all element types for all content/settings and then we can populate any empty properties.
                var allElements = blockEditorData.BlockValue.ContentData.Concat(blockEditorData.BlockValue.SettingsData)
                                  .ToList();
                var allElementTypes = _contentTypeService.GetAll(allElements.Select(x => x.ContentTypeKey).ToArray())
                                      .ToDictionary(x => x.Key);

                foreach (BlockItemData row in allElements)
                {
                    if (!allElementTypes.TryGetValue(row.ContentTypeKey, out IContentType? elementType))
                    {
                        throw new InvalidOperationException($"No element type found with key {row.ContentTypeKey}");
                    }

                    // now ensure missing properties
                    foreach (IPropertyType elementTypeProp in elementType.CompositionPropertyTypes)
                    {
                        if (!row.PropertyValues.ContainsKey(elementTypeProp.Alias))
                        {
                            // set values to null
                            row.PropertyValues[elementTypeProp.Alias] =
                                new BlockItemData.BlockPropertyValue(null, elementTypeProp);
                            row.RawPropertyValues[elementTypeProp.Alias] = null;
                        }
                    }

                    var elementValidation = new ElementTypeValidationModel(row.ContentTypeAlias, row.Key);
                    foreach (KeyValuePair <string, BlockItemData.BlockPropertyValue> prop in row.PropertyValues)
                    {
                        elementValidation.AddPropertyTypeValidation(
                            new PropertyTypeValidationModel(prop.Value.PropertyType, prop.Value.Value));
                    }

                    yield return(elementValidation);
                }
            }
        }