Forum Discussion

Natkinson's avatar
Natkinson
Genius
4 years ago

New Post with Pre-filled form values

Is there any way to create a custom link or button that passes values to the new post page input fields? So, let's say someone is in a TKB for X Product. And they can't find an answer, so they click a button on the page that says "Ask a question about X Product". And it takes them to the New Message form (postpage quilt, I believe) and since they were just in the X Product TKB, we want a tag or label to get automatically added to that input field in the new message form, so it would already have X Product as a label or tag when they type up their question.

Is there any way to do this or are there potential security implications that are preventing this? Our CMs spend a ton of time cleaning up/adding labels/tags and I think something like this would really clean up our Q&A spaces.

  • Inactive User's avatar
    Inactive User

    (Updated this for a little more clarity) 


    The only way I can think of to do this is a little complicated and kludgy, and involves using a combination of Freemarker templates and Javascript/JQuery. 

    If it were me, I would do something like this:

    Create a new component to replace the Add New (Ask a Question about X Product) button, in this case, script.AddLabelsButton.ftl, in order to have the button link to the New Message page with added parameters, i.e., ?parameter1=Xproduct&parameter2=moreInfo and put that component in the proper quilt, replacing the OOTB button with something similar to this:

    <div class="btn">
        <a href="/t5/tkb/articleeditorpage/tkb-id/sample-page/template-id/freeform?myparams=values"/>
    </div> 

    Then in the custom component I've named script.AddLabelsButton.ftl custom component, I would get the parameters from the url via

    <#attempt>
    <#assign param1 = env.context.component.getParameter("param1")!"" />
    <#recover>
    <#assign tagName = "" />
    </#attempt>

    Then in the Javascript/JQuery section of script.AddLabelsButton.ftl I'd find the labels input field and add the new value, something like

    $labelsSet.val($('#lia-labels').val()+'${param1}'); 

    And that should add the label to the article when submitted. Make sure that the settings for the page allows for either custom labels, or that you have the various products/pages as predefined labels if that's what you're using.

    You could do something like that for tags if you prefer. 

    So the final component in completed order would be something like:

    <#attempt>
      <#assign param1 = env.context.component.getParameter("param1")!"" />
      <#recover>
        <#assign param1 = "" />
    </#attempt>
    
    <script>
      ;
      (function ($) {
        $(document).ready(function () {
          $('#lia-labels').val($('#lia-labels').val()+'${param1}');
        });
      })(LITHIUM.jQuery);
    </script>
    
    <div class="btn">
      <a href="/t5/tkb/articleeditorpage/tkb-id/sample-page/template-id/freeform?param1=snacks"/>
    </div>