How to make custom button to be visible or unvisible with plugin code

How to make custom button to be visible or unvisible with plugin code ?

I develop a webitem button in opsbar-transitions.

  • Environment : Jira Server
  • button name : “Approval”

But there is an issue about visibility of the button according to issue status.
So to speaking, I want to hide the button in opsbar-transitions at specific status of issue,
and to show the button in opsbar-transitions at another status of issue.

How can I control the button visibility with java code ?

Anyone you know, please help !
Thanks.

1 Like

Hello, you can use Conditions on Web Items. The documentation is here: https://developer.atlassian.com/server/jira/platform/web-item/#condition-and-conditions-elements

Conditions can be added to the web section, web item and web panel modules, to display them only when all the given conditions are true.

Condition elements must contain a class attribute with the fully-qualified name of a Java class. The referenced class:

  • must implement com.atlassian.plugin.web.Condition, and
  • will be auto-wired by Spring before any condition checks are performed.

Condition elements can take optional parameters. These parameters will be passed in to the condition’s init() method as a map of string key/value pairs after autowiring, but before any condition checks are performed. For example:

I’ve provided a somewhat more complete example stolen from one of my projects.

<conditions>
  <condition class="com.sbehnke.example.MyCustomIssueCondition">
    <param name="project">PROJ</param>
    <param name="issueType">Bug</param>
  </condition>
  <condition class="com.sbehnke.example.MyCustomStatusCondition">
    <param name="status">Open</param>
  </condition>
</conditions>
package com.sbehnke.example;

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.plugin.webfragment.conditions.AbstractWebCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.plugin.PluginParseException;

import javax.inject.Named;
import java.util.Map;

@Named
public class MyCustomIssueCondition extends AbstractWebCondition {
    private String projectKey;
    private String issueTypeName;

    @Override
    public void init(Map<String, String> params) throws PluginParseException {
        super.init(params);

        this.projectKey = params.get("project");
        this.issueTypeName = params.get("issueType");
    }

    @Override
    public boolean shouldDisplay(ApplicationUser applicationUser, JiraHelper jiraHelper) {
        final Issue issue = (Issue) jiraHelper.getContextParams().get("issue");
        if (issue == null) {
            return false;
        }

        final String projectKey = issue.getProjectObject().getKey();
        final String issueTypeName = issue.getIssueType().getName();

        return projectKey.equalsIgnoreCase(this.projectKey) &&
                issueTypeName.equalsIgnoreCase(this.issueTypeName);
    }
}
1 Like

Thanks. I will try ~~

1 Like