umbraco / Umbraco.Forms.Issues

Public issue tracker for Umbraco Forms
29 stars 0 forks source link

Clarification about "ParsePlaceHolders()" in v10+ #1021

Closed hfloyd closed 1 year ago

hfloyd commented 1 year ago

Hi @AndyButland ,

I am in the process of migrating a v7 site to v10. The site has a lot of customized form themes which all need to be updated for the new namespaces/apis. Something I came across was usage of "ParsePlaceHolders()". As per the documentation, I was able to update the code from:

@settings["Caption"].ParsePlaceHolders()

to:

@using Umbraco.Forms.Core.Services;
@inject IPlaceholderParsingService PlaceholderParsingService
...
@PlaceholderParsingService.ParsePlaceHolders(settings["Caption"])

(As per https://docs.umbraco.com/umbraco-forms/developer/magic-strings#how-can-i-parse-these-values-elsewhere-in-my-c-code-or-razor-views & https://www.andybutland.dev/2021/04/umbraco-forms-migration-to-net-core.html)

But I am seeing a code warning message:

Warning (active) CS0618 'IPlaceholderParsingService.ParsePlaceHolders(string, Record?, Form?, Hashtable?)' is obsolete: 'Please use the method overload taking all parameters. This method will be removed in a future version.'

Considering this is being called in a partial with @model Umbraco.Forms.Web.Models.FieldViewModel, the location of those additional parameters isn't obvious to me. Can you provide some additional information or code samples?

Thanks!

nathanwoulfe commented 1 year ago

Hey @hfloyd

The implementation you're looking for is this one, which adds the htmlEncodeValues parameter:

public string ParsePlaceHolders(string value, bool htmlEncodeValues, Record? record = null, Form? form = null, Hashtable? pageElements = null) {}
AndyButland commented 1 year ago

Thanks @nathanwoulfe, yes, that's the one. We added this parameter in a recent update to ensure when we parse placeholders within an HTML block, like a rich text field, we ensure any input provided via the "magic string" is encoded.

hfloyd commented 1 year ago

Thanks, guys, but that wasn't what I was asking.... I was wondering where I would get those parameters : record, form, hashtable while working with a FieldViewModel. Unless you are suggesting that if I use this other overload, the fact that those values are not available won't be an issue?

AndyButland commented 1 year ago

You can just pass null for these to match what you had in your original code, so this should work:

@PlaceholderParsingService.ParsePlaceHolders(settings["Caption"], false)
hfloyd commented 1 year ago

Okay, that clears it up, thank you @AndyButland & @nathanwoulfe ! 🙂