Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Persisting a hidden parameter in a Confluence macro

Pablo Turati
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 8, 2023

Hello Community,

We built a very simple Confluence structured macro and we are trying to add a unique id that is persistent on reloads as well as on edits of the page, for that we have the following example below.  For that we are creating a UUID, if it does not exist. We expect this new UUID to become part of the macro storage format so that it gets picked up on reload.

 

The result should include the new macroId: <ac:parameter ac:name=\"macroId\">%s</ac:parameter> Only it's not.

<p><ac:structured-macro ac:name="radar" ac:schema-version="1" ac:macro-id="4052dd88-dd06-4759-868d-a01add2f8cd5"><ac:parameter ac:name="ProblemClient">chirp</ac:parameter><ac:parameter ac:name="Tool">chart</ac:parameter></ac:structured-macro></p>

 

Also, the body is printing the <ac:parameter> tag as part of the body.

I'm have no idea what could be wrong as so that the <ac:parameter> is not being picked up as part of the meta data.  Any help would be very much appreciated.

 

Screenshot 2023-08-08 at 12.20.57 PM.pngScreenshot 2023-08-08 at 12.23.48 PM.png

 

```java

import com.atlassian.confluence.macro.Macro;
import com.atlassian.confluence.macro.MacroExecutionException;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.webresource.api.assembler.PageBuilderService;
import org.springframework.beans.factory.annotation.Autowired;
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import java.util.Map;
public class radar implements Macro {

private PageBuilderService pageBuilderService;

@Autowired
public radar(
@ComponentImport PageBuilderService pageBuilderService
) {
this.pageBuilderService = pageBuilderService;
}

@Override
public String execute(Map<String, String> map, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException {
pageBuilderService.assembler().resources().requireWebResource("<package>:package-resources");
String macroId = map.get("macroId");

if (StringUtils.isBlank(macroId)) {
macroId = UUID.randomUUID().toString();
map.put("macroId", macroId);
}

return String.format("<p>Your custom macro output</p><ac:parameter ac:name=\"macroId\">%s</ac:parameter>", macroId);
}

public BodyType getBodyType() { return BodyType.NONE; }

public OutputType getOutputType() { return OutputType.BLOCK; }
}
```java
THE XML file is:
<xhtml-macro name="radar" class="private-class-name" key="radar.macro.name" icon="/download/resources/${project.groupId}.${project.artifactId}/images/pluginIcon.png">
<description key="Add this macro to view, edit and create new queries and graphs"/>
<category name="external-content"/>
<parameters>
<parameter name="ProblemClient" type="enum" required="true">
<value name="chirp"/>
<value name="clack"/>
</parameter>
<parameter name="Tool" type="enum" required="true">
<value name="chart"/>
<value name="table"/>
</parameter>
<parameter name="macroId" type="string" hidden="true"/>
</parameters>
<parameters/>
</xhtml-macro>

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events