Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Last active March 13, 2019 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leekelleher/375eccbd5d7891efc16b181db46dde2c to your computer and use it in GitHub Desktop.
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
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);
}
}
}
}
}
@leekelleher
Copy link
Author

Related ticket on the Umbraco issue tracker: http://issues.umbraco.org/issue/CON-1196

@lesley-w
Copy link

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

@StefanoChiodino
Copy link

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