User and date fields

JIRA boasts of a simple, clutterless UI. You will find this theme in the View Issue page, and one of the standout things is the way issue fields are arranged on the page. The fields are grouped based on their type and you will see separate sections for the user and date fields, as shown in the next screenshot:

User and date fields

But how do we define the custom fields that we develop as a date field or a user field?

How to do it...

When you write your new date fields or user fields, all you need to do to make it appear in the correct sections is to implement the right interface!

For a user field, the new custom field type class should implement the following interface:

com.atlassian.jira.issue.fields.UserField

For a date field, implement the following:

com.atlassian.jira.issue.fields.DateField

If you are extending the existing date fields or user fields, they already implement the interface, and hence they will appear automatically in there!

What if you do not want your field in the special date/user sections? Simply ignore these interfaces. The fields will appear just like normal custom fields and will then appear in the order specified under field configurations.

How it works...

This is quite simple. JIRA looks out for classes implementing the UserField/DateField interfaces and displays them in the respective sections. On the standard custom field section, it doesn't show these fields.

Ever wondered where this check is done in the JIRA source code? The view is rendered in the jira-view-issue-plugin-xxx.jar, but the actual check is done in the util class: com.atlassian.jira.issue.fields.util.FieldPredicates.

Here is the sample code for validating Date fields inside the FieldPredicates class:

private static final Predicate<Field> PREDICATE_DATE_FIELD = new CustomFieldTypePredicate(DateField.class);
private static final Predicate<Field> PREDICATE_DATE_CUSTOM_FIELD = Predicates.allOf(PREDICATE_CUSTOM_FIELD, PREDICATE_DATE_FIELD);
/**
* Return a predicate that will return true if the input field is a date field.
*
* @return a predicate that will return true if the input field is a date field.
*/
public static Predicate<Field> isDateField() {
    return FieldPredicates.PREDICATE_DATE_FIELD;
}
/**
* Return a predicate that will return true if the input field is a date custom field.
*
* @return a predicate that will return true if the input field is a date custom field.
*/
public static Predicate<Field> isCustomDateField() {
    return PREDICATE_DATE_CUSTOM_FIELD;
}

You will find similar code for other custom field types, including User custom fields.

See also

  • The Writing a simple custom field recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.138.138