As we have mentioned in the previous recipes, it is possible to add velocity variables while constructing a JIRA web fragment. JIRA supports a list of variables by default, which include user
, req
, baseurl
, and so on. The full list and the details of these variables can be found at https://developer.atlassian.com/display/JIRADEV/Velocity+Contexts.
In this recipe, we will see how to add more variables to the velocity context with the use of the context-provider
element.
The context-provider
element adds to the velocity context available to the web section and web item modules. Only one context-provider
can be added for an item. The following steps show how we can make use of a context provider:
ContextProvider
class.The class must implement com.atlassian.plugin.web.ContextProvider
. To make things easy, it is enough to extend the AbstractJiraContextProvider
class and override the following abstract method in it:
public abstract Map getContextMap(ApplicationUser user, JiraHelperjiraHelper);
The following is what the class looks like if you want to add the full name of the user as a separate variable in the velocity context.
public class UserContextProvider extends AbstractJiraContextProvider { @Override public Map getContextMap(ApplicationUser user, JiraHelper helper) { return MapBuilder.build("userName", user.getDisplayName()); } }
In the example, let us create a new web section with the user's full name in the admin section with a single web item in it to link to the user's website.
<web-section key="jtricks-admin-context-section" name="JTricks Context Section" location="admin_plugins_menu" weight="910"> <label>$userName</label> <context-provider class="com.jtricks.ui.context.UserContextProvider" /> </web-section> <web-item key="jtricks-admin-context-link" name="JTricks Context Link" section="admin_plugins_menu/jtricks-admin-context-section" weight="10"> <label>Website</label> <link linkId="jtricks.admin.context.link"> http://www.j-tricks.com </link> </web-item>
As you can see, the web section refers to $userName
in its label.
52.15.117.231