Last active
March 13, 2019 17:48
-
-
Save leekelleher/375eccbd5d7891efc16b181db46dde2c to your computer and use it in GitHub Desktop.
Umbraco Forms - Extension methods to remove specific providers, e.g. removing a FieldType, such as Recaptcha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using Umbraco.Forms.Core.Common; | |
namespace Our.Umbraco.Web.Extensions | |
{ | |
public static class ProviderCollectionExtensions | |
{ | |
public static void RemoveProvider<T>(this ProviderCollection<T> collection, params string[] providerIds) | |
where T : ProviderBase | |
{ | |
var guids = new List<Guid>(); | |
foreach (var providerId in providerIds) | |
{ | |
if (Guid.TryParse(providerId, out Guid id)) | |
guids.Add(id); | |
} | |
RemoveProvider(collection, guids.ToArray()); | |
} | |
public static void RemoveProvider<T>(this ProviderCollection<T> collection, params Guid[] providerIds) | |
where T : ProviderBase | |
{ | |
var type = typeof(ProviderCollection<T>); | |
var field = type.GetField("m_providers", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); | |
if (field == null) | |
return; | |
var providers = (Dictionary<Guid, T>)field.GetValue(collection); | |
if (providers == null) | |
return; | |
foreach (var providerId in providerIds) | |
{ | |
if (providers.ContainsKey(providerId)) | |
{ | |
providers.Remove(providerId); | |
} | |
} | |
} | |
} | |
} |
Can you provide an example of how to use this to remove the old reCaptcha from the list of available Umbraco Forms field types? I can't figure out how to get it to work.
Thanks
I'm doing RemoveProvider(FieldTypeProviderCollection.Instance, new Guid("fb37bc60-d41e-11de-aeae-37c155d89593"));
in the OnApplicationStarted
of a IApplicationEventHandler
, where that is the guid of the password field that I found debugging. Works nicely, thanks lee!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related ticket on the Umbraco issue tracker: http://issues.umbraco.org/issue/CON-1196