Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Confluence Space Blueprint: How to dynamically add pages to blueprint based on checkboxes in wizard?

Gabriel Jülke November 26, 2015

Hi,

i am new to confluence plugin development struggling with the (not existing) documentation... I have managed so far adding a home page with several variables defined in the wizard. What i would like to achieve now is to add child pages on the base of checkboxes in the wizard. I need to provide several pages but the user should have the ability to choose, which of these pages he needs in the space.

Here is my code so far. If the user checks the "product1" checkbox the product1 page should be added to the space. Otherwise it will not.

atlassian-plugin.xml
...
<space-blueprint key="customer-space-blueprint" i18n-name-key="sector.customer-space.name" category="Kunden">
    <content-template ref="customer-space-blueprint.home">
        <content-template ref="customer-space-blueprint.product1"/>
        <content-template ref="customer-space-blueprint.product2"/>
    </content-template>

    <dialog-wizard key="customer-space-blueprint.wizard">
        ...
        <dialog-page id="wizardProducts"
                     template-key="Sector.SpaceBlueprints.CustomerSpace.wizardProducts"
                     title-key="sector.customer-space.wizard.title"
                     description-header-key="sector.customer-space.wizard.heading"
                     description-content-key="sector.customer-space.wizard.description"
                     last="true"
        />
    </dialog-wizard>
</space-blueprint>

wizardProduct.soy

{namespace Sector.SpaceBlueprints.CustomerSpace}
/**
 * Dialog form template
 *
 * @param atlToken the XSRF token to send with the form
 */
{template .wizardProducts}
<form action="#" method="post" id="decisions-form" class="common-space-form aui">
    <fieldset>
        <div class="field-group">
            <label for="product1">Product1</label>
            <input id="product1" type="checkbox" class="checkbox" name="contentTemplateKey" value="customer-space-blueprint.product1">
        </div>
        <div class="field-group">
            <label for="product2">Product2</label>
            <input id="product2" type="checkbox" class="checkbox" name="contentTemplateKey" value="customer-space-blueprint.product2">
        </div>
    </fieldset>
    <input type="hidden" name="atl_token" value="{$atlToken}" />
</form>
{/template}

4 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Marco Vogel November 14, 2016

Hi Gabriel,

 

I hope this will help you :)

 

 

Right now your space should create all childpages you added in your atlassian-plugin.xml no matter which checkbox ist selected.

 

The solution for my team member an me was to remove all childpages that were not selected when the space is created.

 

To achive this try these steps.

 

Create a java file which extends AbstractBlueprintContextProvider and add the unimplemented methods:

public class AddPages extends AbstractBlueprintContextProvider {

@Override
protected BlueprintContext updateBlueprintContext(BlueprintContext blueprintContext) {
    Object checkbox = blueprintContext.get("ID");
    return blueprintContext;
  }
}

Replace "ID" with the ID you gave your checkbox in the wizardProduct.soy file.

 

Your input element of your checkboxes must contain an id field :

<input type="checkbox" id="checkbox" />

 

Add in your <content-template> sections in atlassian-plugin.xml:

&lt;context-provider class="Path to your Java File "/&gt;

Now you have a reference to your checkbox in the Java file . If the checkbox was not selected the reference will be null.

 

Now we want to know when the space is created. We have to implement an eventlistener to listen for a SpaceCreateEvent: Add the eventlistener in your java class

public class AddPages extends AbstractBlueprintContextProvider {
  Page page, parent;
 
@EventListener
    public void handlePages(PageCreateEvent event) {
        page = event.getPage();
        parent = page.getParent();
        removePages();
    }
 
 
@Override
protected BlueprintContext updateBlueprintContext(BlueprintContext blueprintContext) {
    Object checkbox = blueprintContext.get("ID");
    return blueprintContext;
  }
}

 

In order to use the eventlistener you have to link it with the atlassian-plugin.xml.

Add:

&lt;listener name="Add Pages" key="addpages" class="Path_to_your_java_file"&gt;
   &lt;description&gt;Will listen for user creation events.&lt;/description&gt;
&lt;/listener&gt;

Now the eventlistener gets called when you press "Create" in your Wizard.

 

We need to save the selection states of the checkboxes in the dialog-wizard

Add in your java file

public class AddPages extends AbstractBlueprintContextProvider {
 
 
@EventListener
    public void handlePages(PageCreateEvent event) {
        page = event.getPage();
        parent = page.getParent();
        removePages();
    }
 
 
@Override
protected BlueprintContext updateBlueprintContext(BlueprintContext blueprintContext) {
           Object checkbox;
    
        checkbox = blueprintContext.get("checkbox"); //your ID!
        
        CheckboxesBooleans.setExample(checkbox != null);
        
        return blueprintContext;
  }
}

We created another java class CheckboxesBooleans to keep it simple.

 

public class CheckboxesBooleans {
    private static boolean example=false;
 
    public static boolean getExample() {
        return example;
    }
 
    public static void setExample(boolean example_boolean) {
        example = example_boolean;
    }
}

 

Now we know which checkboxes are selected when the handlePages method gets called.

 

Add the removePages Method to your original java class

private void removePages() {
        if (page.getDisplayTitle().contains("__")) { //your page title
            if (!CheckboxesBooleans.getExample())
                parent.removeChild(page);
        }
    }

This method gets a childpage and checks if the checkbox for the page was not selected. If so the childpage will be removed.

 

I simplified our code for explanation purposes. You have to adapt it to your project.

 

I hope i was able to solve your problem.

 

Greetings

 

Marco

0 votes
Ben Li May 31, 2019

I'd just like to drop in my own take on the issue, now in 2019 - I recently completed a task exactly the same as @Gabriel Jülke's and managed to get everything up and running. Maybe it can help whoever stumbles across this thread.

For the most part, all of @Marco Vogel's tutorial is still applicable in the current development state of confluence server at the time of writing this article. I only had to add a couple of functions in order to get the Listener working with the current Spring components. Specifically, by adding in the DisbosableBean element and it's relevant destroy() function will allow all of the code to run smoothly.

0 votes
pyriand3r November 9, 2016

No, unfortunately i haven't found a solution and i don't have the time any more to dig into this.

0 votes
Alex Smith November 9, 2016

have you found a solution for this?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events