Skip to content

TracTicketsCustomFields

Ricardo Ferraz Leal edited this page Mar 31, 2019 · 12 revisions

<html><body>

Custom Ticket Fields

Trac supports adding custom, user-defined fields to the ticket module. Using custom fields, you can add typed, site-specific properties to tickets.

Configuration

Configuring custom ticket fields is done in the trac.ini file. All field definitions should be under a section named <tt>[ticket-custom]</tt>.

The syntax of each field definition is:

 FIELD_NAME = TYPE
 (FIELD_NAME.OPTION = VALUE)
 ...

The example below should help to explain the syntax.

Available Field Types and Options

  • text : A simple (one line) text field.
    • label: Descriptive label.
    • value: Default value.
    • order: Sort order placement. (Determines relative placement in forms with respect to other custom fields.)
    • format: One of:
      • <tt>plain</tt> for plain text
      • <tt>wiki</tt> to interpret the content as WikiFormatting ( since 0.11.3 )
      • <tt>reference</tt> to treat the content as a queryable value ( since 1.0 )
      • <tt>list</tt> to interpret the content as a list of queryable values, separated by whitespace ( since 1.0 )
  • checkbox : A boolean value check box.
    • label: Descriptive label.
    • value: Default value (0 or 1).
    • order: Sort order placement.
  • select : Drop-down select box. Uses a list of values.
    • label: Descriptive label.
    • options: List of values, separated by | (vertical pipe).
    • value: Default value (one of the values from options).
    • order: Sort order placement.
  • radio : Radio buttons. Essentially the same as select .
    • label: Descriptive label.
    • options: List of values, separated by | (vertical pipe).
    • value: Default value (one of the values from options).
    • order: Sort order placement.
  • textarea : Multi-line text area.
    • label: Descriptive label.
    • value: Default text.
    • cols: Width in columns.
    • rows: Height in lines.
    • order: Sort order placement.
    • format: Either <tt>plain</tt> for plain text or <tt>wiki</tt> to interpret the content as WikiFormatting . ( since 0.11.3 )

Sample Config

[ticket-custom]

test_one = text
test_one.label = Just a text box

test_two = text
test_two.label = Another text-box
test_two.value = Default [mailto:[email protected] owner]
test_two.format = wiki

test_three = checkbox
test_three.label = Some checkbox
test_three.value = 1

test_four = select
test_four.label = My selectbox
test_four.options = one|two|third option|four
test_four.value = two

test_five = radio
test_five.label = Radio buttons are fun
test_five.options = uno|dos|tres|cuatro|cinco
test_five.value = dos

test_six = textarea
test_six.label = This is a large textarea
test_six.value = Default text
test_six.cols = 60
test_six.rows = 30

Note: To make entering an option for a <tt>select</tt> type field optional, specify a leading <tt>|</tt> in the <tt>fieldname.options</tt> option.

Reports Involving Custom Fields

Custom ticket fields are stored in the <tt>ticket_custom</tt> table, not in the <tt>ticket</tt> table. So to display the values from custom fields in a report, you will need a join on the 2 tables. Let's use an example with a custom ticket field called <tt>progress</tt>.

<div class="code">

<span class="k">SELECT</span> p<span class="p">.</span>value <span class="k">AS</span> __color__<span class="p">,</span>
   id <span class="k">AS</span> ticket<span class="p">,</span> summary<span class="p">,</span> <span class="k">owner</span><span class="p">,</span> <span class="k">c</span><span class="p">.</span>value <span class="k">AS</span> progress
  <span class="k">FROM</span> ticket t<span class="p">,</span> enum p<span class="p">,</span> ticket_custom <span class="k">c</span>
  <span class="k">WHERE</span> status <span class="k">IN</span> <span class="p">(</span><span class="s1">'assigned'</span><span class="p">)</span> <span class="k">AND</span> t<span class="p">.</span>id <span class="o">=</span> <span class="k">c</span><span class="p">.</span>ticket <span class="k">AND</span> <span class="k">c</span><span class="p">.</span>name <span class="o">=</span> <span class="s1">'progress'</span>
<span class="k">AND</span> p<span class="p">.</span>name <span class="o">=</span> t<span class="p">.</span>priority <span class="k">AND</span> p<span class="p">.</span><span class="k">type</span> <span class="o">=</span> <span class="s1">'priority'</span>
  <span class="k">ORDER</span> <span class="k">BY</span> p<span class="p">.</span>value

</div>

Note that this will only show tickets that have progress set in them, which is not the same as showing all tickets . If you created this custom ticket field after you have already created some tickets, they will not have that field defined, and thus they will never show up on this ticket query. If you go back and modify those tickets, the field will be defined, and they will appear in the query. If that's all you want, you're set.

However, if you want to show all ticket entries (with progress defined and without), you need to use a <tt>JOIN</tt> for every custom field that is in the query.

<div class="code">

<span class="k">SELECT</span> p<span class="p">.</span>value <span class="k">AS</span> __color__<span class="p">,</span>
   id <span class="k">AS</span> ticket<span class="p">,</span> summary<span class="p">,</span> component<span class="p">,</span> <span class="k">version</span><span class="p">,</span> milestone<span class="p">,</span> severity<span class="p">,</span>
   <span class="p">(</span><span class="k">CASE</span> status <span class="k">WHEN</span> <span class="s1">'assigned'</span> <span class="k">THEN</span> <span class="k">owner</span><span class="o">||</span><span class="s1">' *'</span> <span class="k">ELSE</span> <span class="k">owner</span> <span class="k">END</span><span class="p">)</span> <span class="k">AS</span> <span class="k">owner</span><span class="p">,</span>
   time <span class="k">AS</span> created<span class="p">,</span>
   changetime <span class="k">AS</span> _changetime<span class="p">,</span> description <span class="k">AS</span> _description<span class="p">,</span>
   reporter <span class="k">AS</span> _reporter<span class="p">,</span>
  <span class="p">(</span><span class="k">CASE</span> <span class="k">WHEN</span> <span class="k">c</span><span class="p">.</span>value <span class="o">=</span> <span class="s1">'0'</span> <span class="k">THEN</span> <span class="s1">'None'</span> <span class="k">ELSE</span> <span class="k">c</span><span class="p">.</span>value <span class="k">END</span><span class="p">)</span> <span class="k">AS</span> progress
  <span class="k">FROM</span> ticket t
     <span class="k">LEFT</span> <span class="k">OUTER</span> <span class="k">JOIN</span> ticket_custom <span class="k">c</span> <span class="k">ON</span> <span class="p">(</span>t<span class="p">.</span>id <span class="o">=</span> <span class="k">c</span><span class="p">.</span>ticket <span class="k">AND</span> <span class="k">c</span><span class="p">.</span>name <span class="o">=</span> <span class="s1">'progress'</span><span class="p">)</span>
     <span class="k">JOIN</span> enum p <span class="k">ON</span> p<span class="p">.</span>name <span class="o">=</span> t<span class="p">.</span>priority <span class="k">AND</span> p<span class="p">.</span><span class="k">type</span><span class="o">=</span><span class="s1">'priority'</span>
  <span class="k">WHERE</span> status <span class="k">IN</span> <span class="p">(</span><span class="s1">'new'</span><span class="p">,</span> <span class="s1">'assigned'</span><span class="p">,</span> <span class="s1">'reopened'</span><span class="p">)</span>
  <span class="k">ORDER</span> <span class="k">BY</span> p<span class="p">.</span>value<span class="p">,</span> milestone<span class="p">,</span> severity<span class="p">,</span> time

</div>

Note in particular the <tt>LEFT OUTER JOIN</tt> statement here.

Updating the database

As noted above, any tickets created before a custom field has been defined will not have a value for that field. Here's a bit of SQL (tested with SQLite) that you can run directly on the Trac database to set an initial value for custom ticket fields. Inserts the default value of 'None' into a custom field called 'request_source' for all tickets that have no existing value:

<div class="code">

<span class="k">INSERT</span> <span class="k">INTO</span> ticket_custom
   <span class="p">(</span>ticket<span class="p">,</span> name<span class="p">,</span> value<span class="p">)</span>
   <span class="k">SELECT</span> 
      id <span class="k">AS</span> ticket<span class="p">,</span>
      <span class="s1">'request_source'</span> <span class="k">AS</span> name<span class="p">,</span>
      <span class="s1">'None'</span> <span class="k">AS</span> value
   <span class="k">FROM</span> ticket 
   <span class="k">WHERE</span> id <span class="k">NOT</span> <span class="k">IN</span> <span class="p">(</span>
      <span class="k">SELECT</span> ticket <span class="k">FROM</span> ticket_custom
   <span class="p">);</span>

</div>

If you added multiple custom fields at different points in time, you should be more specific in the subquery on table <tt>ticket</tt> by adding the exact custom field name to the query:

<div class="code">

<span class="k">INSERT</span> <span class="k">INTO</span> ticket_custom
   <span class="p">(</span>ticket<span class="p">,</span> name<span class="p">,</span> value<span class="p">)</span>
   <span class="k">SELECT</span> 
      id <span class="k">AS</span> ticket<span class="p">,</span>
      <span class="s1">'request_source'</span> <span class="k">AS</span> name<span class="p">,</span>
      <span class="s1">'None'</span> <span class="k">AS</span> value
   <span class="k">FROM</span> ticket 
   <span class="k">WHERE</span> id <span class="k">NOT</span> <span class="k">IN</span> <span class="p">(</span>
      <span class="k">SELECT</span> ticket <span class="k">FROM</span> ticket_custom <span class="k">WHERE</span> name <span class="o">=</span> <span class="s1">'request_source'</span>
   <span class="p">);</span>

</div>


See also: TracTickets , TracIni

</body></html>

Clone this wiki locally