Developer forum

Forum » PIM » Multivalue fields and FieldTypeProviders.ConcatProvider

Multivalue fields and FieldTypeProviders.ConcatProvider

Michael Knudsen
Reply

Hello forum,

- I'm trying to use the Dynamicweb.Ecommerce.FieldTypeProdviders.ConcatProvider to generate a bullet list of product properties. Everything works fine, but can multivalue fields generate separat values in a bullet list?

Fields:
Area: Indoor,Outdoor
Height: 20 cm
Width: 30 cm
Depth: 40 cm

Concat field:
Properties:
<ul>
  <li>Can be used: {Area}</li>
  <li>{Height} x {Width} x {Depth}</li>
</ul>

Expected output (if possible?) of my ConCat field:
Properties:

  • Can be used: Indoor
  • Can be used: Outdoor
  • 20 cm x 30 cm x 40 cm

 

Br. Michael Knudsen


Replies

 
Nicolai Pedersen
Reply

No - you would have to change how the concat provider works then...

You can modify it to your use case and use your custom version of it.

Code here:

namespace Dynamicweb.Ecommerce.FieldTypeProviders
{
    [AddInName(ProviderAddinName)]
    [AddInLabel("Concat FieldType Provider")]
    [AddInActive(true)]
    [Serializable]
    public class ConcatProvider : ProductFieldTypeProvider, IDropDownOptions, IExpressionEditorDynamicHeight
    {
        private enum DisplayTypes { InputText, InputTextArea }

        private DisplayTypes displayType = DisplayTypes.InputText;

        internal const string ProviderAddinName = "Dynamicweb.Ecommerce.FieldTypeProviders.ConcatProvider";

        [AddInParameter("Expression")]
        [AddInParameterEditor(typeof(ExpressionListParameterEditor), "")]
        public string Expression { get; set; } = string.Empty;

        [AddInParameter("Displaytype"), AddInLabel("Display type")]
        [AddInParameterEditor(typeof(RadioParameterEditor), "")]
        public string DisplayType
        {
            get { return displayType.ToString(); }
            set { Enum.TryParse<DisplayTypes>(value, out displayType); }
        }

        private Dictionary<string, string> _expressionPerLanguage;
        private Dictionary<string, string> ExpressionPerLanguage
        {
            get
            {
                if (_expressionPerLanguage == null)
                {
                    _expressionPerLanguage = JsonConverter.Current.Deserialize<Dictionary<string, string>>(Expression);
                    if (_expressionPerLanguage == null)
                    {
                        _expressionPerLanguage = new Dictionary<string, string>();
                    }
                }
                return _expressionPerLanguage;
            }
        }

        public string GetExpression(string language)
        {
            if (language == null)
            {
                return ExpressionPerLanguage.Values.FirstOrDefault() ?? string.Empty;
            }
            string label = string.Empty;
            ExpressionPerLanguage.TryGetValue(language, out label);
            return label;
        }

        public override object GetProductValue(Product product, object fieldValue, string languageId, Dictionary<string, string> settings)
        {
            if (product == null)
            {
                return fieldValue;
            }
            var json = string.Empty;
            if (string.IsNullOrEmpty(Expression) && settings.TryGetValue(nameof(Expression), out json))
            {
                Expression = json;
            }
            var templateInfo = GetExpression(product.LanguageId);
            if (string.IsNullOrEmpty(templateInfo))
            {
                var defaultLanguageId = Ecommerce.Services.Languages.GetDefaultLanguageId();
                if (product.LanguageId != defaultLanguageId)
                {
                    templateInfo = GetExpression(product.LanguageId);
                    if (string.IsNullOrEmpty(templateInfo))
                    {
                        templateInfo = GetExpression(null);
                    }
                }
            }
            var fieldValuePlaceholders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            foreach (var fieldPair in ProductField.GetAllEditableProductFields())
            {
                var placeHolder = $"{{{fieldPair.Key}}}";
                if (templateInfo.IndexOf(placeHolder, StringComparison.OrdinalIgnoreCase) > -1)
                {
                    var fieldVal = string.Empty;
                    var fieldValResult = Services.Products.GetFieldValue(product, fieldPair.Value);
                    if (fieldValResult.Succeeded)
                    {
                        fieldVal = Converter.ToString(fieldValResult.Data);
                    }
                    fieldValuePlaceholders[placeHolder] = fieldVal;
                }

            }

            var expr = @"(\[[^\[\]]*\])";
            var regex = new Regex(expr);
            if (regex.IsMatch(templateInfo))
            {
                var result = regex.Replace(templateInfo, (_match) =>
                {
                    var groupVal = string.Empty;
                    var tpl = _match.Value;
                    if (!string.IsNullOrEmpty(tpl))
                    {
                        tpl = tpl.Trim(new[] { '[', ']' });
                        foreach (var placeHolder in fieldValuePlaceholders.Keys)
                        {
                            if (tpl.IndexOf(placeHolder, StringComparison.OrdinalIgnoreCase) > -1 && !string.IsNullOrWhiteSpace(fieldValuePlaceholders[placeHolder]))
                            {
                                groupVal = tpl.Replace(Regex.Escape(placeHolder), fieldValuePlaceholders[placeHolder], RegexOptions.IgnoreCase);
                                break;
                            }
                        }
                    }
                    return groupVal;
                });

                return result;
            }
            else {
                foreach (var placeHolder in fieldValuePlaceholders.Keys)
                {
                    templateInfo = templateInfo.Replace(Regex.Escape(placeHolder), fieldValuePlaceholders[placeHolder], RegexOptions.IgnoreCase);
                }
                return templateInfo;
            }
        }

        public override bool IsCalculated(Dictionary<string, string> settings)
        {
            return true;
        }

        public override object GetValue(object value, string languageId, Dictionary<string, string> settings)
        {
            return value;
        }

        public override string GetAppenderInfo(string languageId, string settings)
        {
            return null;
        }

        public override string GetAppenderInfo(string languageId, Dictionary<string, string> settings)
        {
            return null;
        }

        protected override void Render(TextWriter writer, string id, bool enabled, string value, bool hasError, string text, string title)
        {
            Control control;
            if (displayType == DisplayTypes.InputText)
            {

                control = new InputText()
                {
                    ID = id,
                    Disabled = IsCalculated(null),
                    Value = string.IsNullOrWhiteSpace(value) ? default(string) : Converter.ToString(value),
                    ValidationMessage = hasError ? Translate.Translate("This field is required") : null,
                };
            }
            else
            {
                control = new InputTextArea()
                {
                    ID = id,
                    Disabled = IsCalculated(null),
                    Value = string.IsNullOrWhiteSpace(value) ? default(string) : Converter.ToString(value),
                    ValidationMessage = hasError ? Translate.Translate("This field is required") : null,
                    Rows = 5
                };
            }
            using (var htmlTextWriter = new HtmlTextWriter(writer))
            {
                control.RenderControl(htmlTextWriter);
            }
        }

        public override void RenderAdditionalContent(TextWriter output)
        {
            var defaultLanguageInputId = $"{nameof(Expression)}_{Ecommerce.Services.Languages.GetDefaultLanguageId()}";
            CalculationProvider.RenderDefaultLanguageUiValidation(output, defaultLanguageInputId);
        }

        public Hashtable GetOptions(string dropdownName)
        {
            var options = new Hashtable();
            if (dropdownName.Equals("DisplayType", StringComparison.OrdinalIgnoreCase))
            {
                return new Hashtable
                                   {
                                       {DisplayTypes.InputText.ToString(), Translate.Translate("Single line")},
                                       {DisplayTypes.InputTextArea.ToString(), Translate.Translate("Multi line")}
                                   };
            }
            return options;
        }

        /// <summary>
        /// Gets row count of expession editor text area control
        /// </summary>
        public int GetExpressionEditorHeight()
        {
            return displayType == DisplayTypes.InputText ? 2 : 6;
        }
    }
}

 

You must be logged in to post in the forum