DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

How to create custom token replacement provider

 4 Replies
 2 Subscribed to this topic
 18 Subscribed to this forum
Sort:
Author
Messages
New Around Here
Posts:
New Around Here

    Hi all,

    In the latest versions of DNN there is a interface to create custom token repleacemet provider. I want to creat my custom token replacement provider. Are the any examples of this or is there any documentation on how to implement this?

    Growing Member
    Posts: 45
    Growing Member

      Hi Joe,
      I've been looking into that too, recently. It looks like you can just create your own TokenProvider class, make it inherit DotNetNuke.Services.Tokens.TokenProvider and register it in web dot config as the default one.

          public class JoesTokenProvider : TokenProvider
          {
              /// 
              public override bool ContainsTokens(string content, TokenContext context)
              {
                  return false; // already determined by BaseCustomTokenReplace
              }
      
              /// 
              public override string Tokenize(string content, TokenContext context)
              {
                  var tokenizer = new TokenReplace { TokenContext = context };
                  return tokenizer.ReplaceEnvironmentTokens(content);
              }
          }

      Then you can implement it however you want to implement it.

      But I haven't tried it yet myself either...

      Greetz,
      Stefan

       

      Veteran Member
      Posts: 763
      Veteran Member

        Sounds cool!

        From what version on is 'Latest versions'?
        And where does it reside?

        New Around Here
        Posts: 4
        New Around Here
          Hi Joe,

          I believe I did that initial implementation to better support tokenization across all modules for our Tokens Builder (formerly known as My Tokens).

          We derive ours from DotNetNuke.Services.Tokens.TokenProvider, same as the built-in one (CoreTokenProvider).
          Note that you'll also need to configure web config to use your provider, which if I'm not mistaken will override the default one. So make sure to inherit all built-in functionality.

          Would you be interested to see the implementation we have in Plant an App?
          Basically, it eliminates the need to do your own provider as new custom tokens can be created through an UI interface and become available in all modules.
          Growing Member
          Posts: 92
          Growing Member
            Doing this from my phone but will try to give you some info. We added this to the forums module which you can see at
            https://github.com/DNNCom.../Dnn.CommunityForums

            First, you implement DotNetNuke.Services.Tokens.BaseCustomTokenReplace, which is the provider/router to handle replacement requests. Based on how you configure the property sources, the replacement requests are routed to each source (object) which implements DotNetNuke.Services.Tokens.IPropertyAccess. You implement a method GetProperty, which receives a property name, a format string, and other parameters.

            If you want to see a simple implementation, see Dnn.CommunityForums/Services/Tokens/ForumsModuleTokenReplacer.cs

            This implementation handles [DCF:*] tokens. We also have a ton more code here: Dnn.CommunityForums/Services/Tokens/TokenReplacer.cs, which implements token replacement for [FORUM:*], [FORUMTOPIC:*], etc.

            One HUGE caveat. When you pass whatever source text containing the tokens, if a token is not recognized, it is removed. So you have to include property sources for USER:, PORTAL:, etc. if your source will include those tokens.

            Also, nested tokens are not allowed but we implemented that for the forums module so that tokens could be embedded in the format string of a token. If you want more information on forums tokens, see https://github.com/DNNCom.../wiki/TokenReference


            These Forums are dedicated to the discussion of DNN Platform.

            For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

            1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
            2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
            3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
            4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
            5. No Flaming or Trolling.
            6. No Profanity, Racism, or Prejudice.
            7. Site Moderators have the final word on approving / removing a thread or post or comment.
            8. English language posting only, please.

            Would you like to help us?

            Awesome! Simply post in the forums using the link below and we'll get you started.

            Get Involved