Well, I have chosen this as the first topic because of the most obvious reason. This is what most of the people will start with. That is one reason why there are lot of customfield plug-ins around! No matter how many are there, you will need a new type. You always do!
Given the context, let me make a huge assumption - You know how to create a skeleton plugin! If not, go to here and do it before you continue!
So we are ready with the Skeleton. And I hope you already know what is atlassian-plugin.xml . Even if you don't, you already have a skeleton and I will tell you what you need for a custom field, I mean for my sample custom field!
For the example, I chose to create a Read Only customfield that stores the last edited user name! It is simple in functionality and enough to explain the basic concepts. So here we go: Modify the atlassin-plugin.xml to inclue the customfield module as follows. You will define the custom field class and the related velocity templates here!
<customfield-type key="readonly-user" name="Read Only User CF" class="com.jtricks.ReadOnlyUserCF"> <description>Read Only User CF Description</description> <resource type="velocity" name="view" location="templates/com/jtricks/view-readonly-user.vm" /> <resource type="velocity" name="column-view" location="templates/com/jtricks/view-readonly-user.vm" /> <resource type="velocity" name="xml" location="templates/com/jtricks/view-readonly-user.vm" /> <resource type="velocity" name="edit" location="templates/com/jtricks/edit-readonly-user.vm" /> </customfield-type>
The structure is the same for all custom fields. You must have a unique key which is 'readonly-user' in this case. Class is the most important thing which could be one of the JIRA custom field types. In this case I have chosen to write a class of my own just to explain the concepts. The class 'ReadOnlyUserCF' extends JIRA's TextCFType and will be explained later.
The next important thing is the velocity templates. You can define different or same templates for each functionalities. In this case, I have chosen to use the same for view, column-view and xml. view template is used in the 'View Issue' page, column-view template in 'Issue Navigator' & reports and xml in XML exports. For edit, I have chosen a different template as you can see there. Let's have a look at the class!
public class ReadOnlyUserCF extends TextCFType{ private final JiraAuthenticationContext authContext; public ReadOnlyUserCF(CustomFieldValuePersister customFieldValuePersister, StringConverter stringConverter, GenericConfigManager genericConfigManager, JiraAuthenticationContext authContext) { super(customFieldValuePersister, stringConverter, genericConfigManager); this.authContext = authContext; } @Override public Map getVelocityParameters(Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem){ Map params = super.getVelocityParameters(issue, field, fieldLayoutItem); params.put("currentUser", authContext.getUser().getName()); return params; } }
The class extends TextCFType as our custom field in essence is a text field. You can extend SelectCFType if you are creating a custom select list and so on! The IDE will automatically prompt you to create a constuctor as the super class needs to be injected with the relevant parameters. I have injected an additional parameter 'JiraAuthenticationContext' in the constuctor here as it is required in our plug-in.
Now, you need to populate the velocity context with the variables you need. For that, all you need to do is to override the 'getVelocityParameters' method. This returns you a Map with lots of populated params and you can add additional key-value pairs into it as I have added 'currentUser'
params.put("currentUser", authContext.getUser().getName());
We have now the class ready with an additional parameter added into the velocity context! A list of parameters already in there can be found here.
Note: As you can see, 'authcontext' is already in there and I don't need an extra variable to be put in there as I can retieve the current user in the vm from this variable. I have done this just for the sake of the tutorial! Now let's move no to the templates. The variables in the context is shared across all the templates. Let us have a look at the edit template.
<input type="text" name="$customField.id" value="$currentUser" id="$customField.id" class="textfield" readonly="readonly" />
Here the highlighted currentUser is the variable we defined in the context in our class. the text field is marked as readonly as well! $customfield automatically comes from the context and we should kepp it as JIRA uses it to update the value back when you press UPDATE!
Note: $authContext.user.name will give you the same result as $currentUser That is it for edit template. View templates looks like this:
#if ($value) $value #end
That's it! Simply displays the current value of the custom field. More complex things can be added if you want beautification, data conversions etc.
And now, we have our custom field ready!! Package it and deploy it into jira-home/plugins/installed-plugins (WEB-INF/lib if you created plugin-1 version). And see if it works If you need searching enabled, you need to tie it up with a JIRA's built in Searcher or one you have written! More of that and more about custom fields plugins be found here. And more on CustomFields here. Njoy! Finf the full source code below. And feel free to post your comments/feedback!
46 Comments
Anonymous
9/30/2010 07:43:19 pm
This is useful.
Reply
csytsma
10/19/2010 04:10:44 am
Very helpful, thanks for posting these. Looking forward to reading the rest of your tutorials.
Reply
J-Tricks
10/19/2010 04:17:58 am
Thank you! Keep watching this space :)
Reply
Jamie
1/24/2011 10:04:11 pm
thank you for a lot of great tutorials, they've helped me a lot. The one thing I havn't been able to figure out is how to set the value of the custom text field. Can you give me any pointers?
Reply
J-Tricks
1/24/2011 11:19:38 pm
Thanks Jamie.
Reply
Ajay Singh
1/27/2011 07:38:14 am
Thanks Jobin.. I found one very good pointer in your tutorial!
Reply
J-Tricks
1/27/2011 07:54:44 am
Thanks Ajay. Glad to know it helped!
Reply
sri
4/3/2011 08:18:18 pm
Hi J-tricks,
Reply
J-Tricks
4/13/2011 07:50:20 am
Hello Sri,
Reply
konsta
8/23/2011 06:07:07 pm
Hi,
Reply
J-Tricks
8/25/2011 03:02:18 pm
@Konsta Same template for view and edit? Also, which version of JIRA is it? The control header is different for the latest versions.
Reply
Rambanam
10/16/2011 05:31:23 pm
Hi,
Reply
J-Tricks
10/17/2011 04:50:11 am
@Rambanam Its is difficult to write tutorials for individual field types because there are a lot. We are hoping just to cover the basics. However couple with this tutorial, have a look at the MultiSelectCF type in JIRA and you should be able to work it out.
Reply
Oliver
11/5/2011 03:16:45 am
Great Example. However does anybody know how to SQL query from a Custom field ?
Reply
J-Tricks
11/5/2011 11:10:45 am
Hello Oliver,
Reply
mizan
12/26/2011 09:40:30 pm
Hi,
Reply
Sathish
1/3/2012 09:26:45 pm
I've used the sample code given. Added & Enabled this plugin in my Jira. But i'm not able to see Read Only User CF custom field in Custom field page. is there any other configuration needs to be done.
Reply
J-Tricks
1/4/2012 12:51:05 am
Are you looking at Administration > Custom fields? You need to create a custom field of the type 'Read Only CF' as mentioned at http://confluence.atlassian.com/display/JIRA/Adding+a+Custom+Field.
Reply
Sathish
1/4/2012 08:40:45 pm
CLass
J-Tricks
1/5/2012 01:21:27 am
Can you answer the above question please? What is the error on logs? And what do you mean by custom fields page? Viewing the list of fields or adding a new one?
Reply
Sathish
1/5/2012 01:53:28 am
I didn't get any Error.
Reply
J-Tricks
1/5/2012 01:58:00 am
Sathish, You wouldn't see the field in the list of custom fields unless it is added. See http://confluence.atlassian.com/display/JIRA/Adding+a+Custom+Field for adding a custom field.
Sathish
1/6/2012 12:27:35 am
Thanks It Worked.
Joe Caputo
1/30/2012 06:53:36 am
Hi there,
Reply
J-Tricks
2/5/2012 02:53:05 pm
Maybe I am missing the obvious here but can't you read the file by giving the absolute path as long as it is on the file system?
Reply
Sathish
2/8/2012 05:02:49 pm
Hi I Need A Help In Jira Data Base Schema.
Reply
J-Tricks
2/9/2012 12:38:44 am
It is indeed the counter value that you need to change. Make sure you restart JIRA after you do that.
Reply
Sathish`
2/9/2012 05:34:19 pm
Thanks Jobin, It Worked.
Sridhar
7/13/2012 08:12:10 am
1. How can I add a text on the form . I mean just a Field name without field type and I am not looking for label. Is there anyway I could add HTML Label on project basis, not as a global field.
Reply
2/6/2013 07:52:05 am
Great tutorial! Thank you for taking the time and explaining basic concepts of custom field creation!
Reply
J-Tricks
2/7/2013 11:15:33 am
Glad to know you like the tutorials!
Reply
RamplingAP
2/11/2013 01:04:57 pm
Thanks for pointing me in the right direction... I think this might just do the trick!
Lincy
3/11/2014 03:00:09 am
Hi Jobin,
Reply
J-Tricks
3/11/2014 04:31:35 pm
Interesting requirement. Haven't tried that though. Maybe in another post ;)
Reply
Lincy
3/13/2014 11:06:21 pm
Thank you, that would be helpful. Meanwhile could you please help me with some pointers on how to achive this?
Michal
8/11/2014 09:30:09 pm
I created new Custom Field and in the implementation of any methods
Reply
Ankit
5/23/2015 12:01:34 am
Hi,
Reply
J-Tricks
5/23/2015 01:54:43 am
Glad to know you liked the tutorial.
Reply
Ankit
5/26/2015 11:51:44 pm
Thanks a lot!
Kiran Jonnada
2/6/2019 04:47:42 pm
Is it possible for us to construct view page similar to the edit page functionality?
Reply
J-Tricks
2/7/2019 04:08:11 pm
Yes. Once you decide to write your own custom field, you have full control on all the views. Having said that, it is better to follow the overall JIRA UI norms for better user experience.
Reply
Nisarg
7/25/2019 02:09:23 am
Hello J-Tricks,
Reply
J-Tricks
7/25/2019 10:17:28 pm
Checkboxes are using MultiSelectCFType class. Only the templates are different. So, you can extend MultiSelectCFType.
Reply
Jan
12/1/2020 03:56:55 pm
Hey J-Tricks
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|