Wiki source code of Add a JIRA Field Displayer as a Wiki Component
Last modified by Vincent Massol on 2026/08/04 21:49
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | This tutorial builds a field renderer for the ##jira## Macro entirely in a wiki page, with no JAR to compile. It contributes a new field type named ##lien##, which displays the value JIRA returned as a link labelled "Lien", and it reuses the built-in renderer to obtain that value. Writing it needs the [[Wiki Component Module>>extensions:Extension.WikiComponent Module]] and programming rights on the page holding it. | ||
| 2 | |||
| 3 | 1. Create a page named ##XWiki.LienJIRAFieldDisplayer##, whose name is free, and edit its objects. | ||
| 4 | 1. Add an ##XWiki.ComponentClass## xobject, set its "Component Role Type" to ##org.xwiki.contrib.jira.macro.JIRAFieldDisplayer##, its "Component Role Hint" to ##type/lien##, and its "Component Scope" to "Current Wiki".((( | ||
| 5 | {{image reference="wikicomponent-1.png" size="large" alt="An XWiki.ComponentClass xobject whose role type is JIRAFieldDisplayer and whose role hint is type slash lien"/}} | ||
| 6 | |||
| 7 | The hint has the form ##type/<field type>## because the renderer is registered for a field **type** rather than for one field id. | ||
| 8 | ))) | ||
| 9 | 1. Add an ##XWiki.ComponentDependencyClass## xobject, set its "Dependency Role Type" to ##org.xwiki.contrib.jira.macro.JIRAFieldDisplayer##, its "Dependency Role Hint" to ##default##, and its "Binding name" to ##defaultDisplayer##.((( | ||
| 10 | {{image reference="wikicomponent-2.png" size="large" alt="An XWiki.ComponentDependencyClass xobject bound under the name defaultDisplayer to the default JIRAFieldDisplayer"/}} | ||
| 11 | |||
| 12 | This makes the built-in renderer available to the code of the next step, which uses it to read the raw field value instead of parsing the issue itself. | ||
| 13 | ))) | ||
| 14 | 1. Add an ##XWiki.ComponentMethodClass## xobject, set its "Method name" to ##displayField##, and put this code in its "Method body code".((( | ||
| 15 | {{code language="groovy"}} | ||
| 16 | {{groovy}} | ||
| 17 | import org.xwiki.rendering.block.* | ||
| 18 | import org.xwiki.rendering.listener.reference.* | ||
| 19 | |||
| 20 | def field = xcontext.method.input.get(0) | ||
| 21 | def issue = xcontext.method.input.get(1) | ||
| 22 | def parameters = xcontext.method.input.get(2) | ||
| 23 | String text = xcontext.method.defaultDisplayer.displayField(field, issue, parameters).get(0).getProtectedString() | ||
| 24 | |||
| 25 | def reference = new ResourceReference(text, ResourceType.URL) | ||
| 26 | def labelBlocks = [new WordBlock("Lien")] | ||
| 27 | xcontext.method.output.value = [new LinkBlock(labelBlocks, reference, true)] | ||
| 28 | {{/groovy}} | ||
| 29 | {{/code}} | ||
| 30 | |||
| 31 | {{image reference="wikicomponent-3.png" size="large" alt="An XWiki.ComponentMethodClass xobject named displayField holding the Groovy implementation"/}} | ||
| 32 | |||
| 33 | The three arguments of ##displayField## arrive in ##xcontext.method.input##, the dependency bound in the previous step is reached as ##xcontext.method.defaultDisplayer##, and the blocks to render are returned through ##xcontext.method.output.value##. | ||
| 34 | ))) | ||
| 35 | 1. Save the page, which registers the Component immediately. | ||
| 36 | 1. Edit any page and call the ##jira## Macro with a field declaring the new type, as in ##fields="key,summary,link!lien"##. The renderer turns the value of the field into a URL, so pick a field holding one, such as the standard ##link## field. | ||
| 37 | 1. Display the page. The field is rendered as a link labelled "Lien".((( | ||
| 38 | {{image reference="wikicomponent-result.png" size="large" alt="A table of JIRA issues whose last column is rendered by the new field type as a link labelled Lien"/}} | ||
| 39 | ))) |