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 hidden or 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); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm doing
RemoveProvider(FieldTypeProviderCollection.Instance, new Guid("fb37bc60-d41e-11de-aeae-37c155d89593"));in theOnApplicationStartedof aIApplicationEventHandler, where that is the guid of the password field that I found debugging. Works nicely, thanks lee!