| 1 | = Custom Ticket Fields = |
| 2 | Trac supports adding custom, user-defined fields to the ticket module. Using custom fields, you can add typed, site-specific properties to tickets. |
| 3 | |
| 4 | == Configuration == |
| 5 | Configuring custom ticket fields is done in the [wiki:TracIni trac.ini] file. All field definitions should be under a section named `[ticket-custom]`. |
| 6 | |
| 7 | The syntax of each field definition is: |
| 8 | {{{ |
| 9 | FIELD_NAME = TYPE |
| 10 | (FIELD_NAME.OPTION = VALUE) |
| 11 | ... |
| 12 | }}} |
| 13 | The example below should help to explain the syntax. |
| 14 | |
| 15 | === Available Field Types and Options === |
| 16 | * '''text''': A simple (one line) text field. |
| 17 | * label: Descriptive label. |
| 18 | * value: Default value. |
| 19 | * order: Sort order placement. (Determines relative placement in forms with respect to other custom fields.) |
| 20 | * '''checkbox''': A boolean value check box. |
| 21 | * label: Descriptive label. |
| 22 | * value: Default value (0 or 1). |
| 23 | * order: Sort order placement. |
| 24 | * '''select''': Drop-down select box. Uses a list of values. |
| 25 | * label: Descriptive label. |
| 26 | * options: List of values, separated by '''|''' (vertical pipe). |
| 27 | * value: Default value (one of the values from options). |
| 28 | * order: Sort order placement. |
| 29 | * '''radio''': Radio buttons. Essentially the same as '''select'''. |
| 30 | * label: Descriptive label. |
| 31 | * options: List of values, separated by '''|''' (vertical pipe). |
| 32 | * value: Default value (one of the values from options). |
| 33 | * order: Sort order placement. |
| 34 | * '''textarea''': Multi-line text area. |
| 35 | * label: Descriptive label. |
| 36 | * value: Default text. |
| 37 | * cols: Width in columns. |
| 38 | * rows: Height in lines. |
| 39 | * order: Sort order placement. |
| 40 | |
| 41 | === Sample Config === |
| 42 | {{{ |
| 43 | [ticket-custom] |
| 44 | |
| 45 | test_one = text |
| 46 | test_one.label = Just a text box |
| 47 | |
| 48 | test_two = text |
| 49 | test_two.label = Another text-box |
| 50 | test_two.value = Just a default value |
| 51 | |
| 52 | test_three = checkbox |
| 53 | test_three.label = Some checkbox |
| 54 | test_three.value = 1 |
| 55 | |
| 56 | test_four = select |
| 57 | test_four.label = My selectbox |
| 58 | test_four.options = one|two|third option|four |
| 59 | test_four.value = two |
| 60 | |
| 61 | test_five = radio |
| 62 | test_five.label = Radio buttons are fun |
| 63 | test_five.options = uno|dos|tres|cuatro|cinco |
| 64 | test_five.value = dos |
| 65 | |
| 66 | test_six = textarea |
| 67 | test_six.label = This is a large textarea |
| 68 | test_six.value = Default text |
| 69 | test_six.cols = 60 |
| 70 | test_six.rows = 30 |
| 71 | }}} |
| 72 | |
| 73 | ''Note: To make entering an option for a `select` type field optional, specify a leading `|` in the `fieldname.options` option.'' |
| 74 | |
| 75 | === Reports Involving Custom Fields === |
| 76 | |
| 77 | Custom ticket fields are stored in the `ticket_custom` table, not in the `ticket` 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 `progress`. |
| 78 | |
| 79 | {{{ |
| 80 | #!sql |
| 81 | id AS ticket, summary, owner, c.value AS progress |
| 82 | FROM ticket t, enum p, ticket_custom c |
| 83 | WHERE status IN ('assigned') AND t.id = c.ticket AND c.name = 'progress' |
| 84 | AND p.name = t.priority AND p.type = 'priority' |
| 85 | ORDER BY p.value |
| 86 | }}} |
| 87 | '''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. |
| 88 | |
| 89 | However, if you want to show all ticket entries (with progress defined and without), you need to use a `JOIN` for every custom field that is in the query. |
| 90 | {{{ |
| 91 | #!sql |
| 92 | id AS ticket, summary, component, version, milestone, severity, |
| 93 | (CASE status WHEN 'assigned' THEN owner||' *' ELSE owner END) AS owner, |
| 94 | time AS created, |
| 95 | changetime AS _changetime, description AS _description, |
| 96 | reporter AS _reporter, |
| 97 | (CASE WHEN c.value = '0' THEN 'None' ELSE c.value END) AS progress |
| 98 | FROM ticket t |
| 99 | LEFT OUTER JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'progress') |
| 100 | JOIN enum p ON p.name = t.priority AND p.type='priority' |
| 101 | WHERE status IN ('new', 'assigned', 'reopened') |
| 102 | ORDER BY p.value, milestone, severity, time |
| 103 | }}} |
| 104 | |
| 105 | Note in particular the `LEFT OUTER JOIN` statement here. |
| 106 | |
| 107 | === Updating the database === |
| 108 | |
| 109 | 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: |
| 110 | |
| 111 | {{{ |
| 112 | #!sql |
| 113 | INSERT INTO ticket_custom |
| 114 | (ticket, name, value) |
| 115 | SELECT |
| 116 | id AS ticket, |
| 117 | 'request_source' AS name, |
| 118 | 'None' AS value |
| 119 | FROM ticket |
| 120 | WHERE id NOT IN ( |
| 121 | SELECT ticket FROM ticket_custom |
| 122 | ); |
| 123 | }}} |
| 124 | |
| 125 | If you added multiple custom fields at different points in time, you should be more specific in the subquery on table {{{ticket}}} by adding the exact custom field name to the query: |
| 126 | |
| 127 | {{{ |
| 128 | #!sql |
| 129 | INSERT INTO ticket_custom |
| 130 | (ticket, name, value) |
| 131 | SELECT |
| 132 | id AS ticket, |
| 133 | 'request_source' AS name, |
| 134 | 'None' AS value |
| 135 | FROM ticket |
| 136 | WHERE id NOT IN ( |
| 137 | SELECT ticket FROM ticket_custom WHERE name = 'request_source' |
| 138 | ); |
| 139 | }}} |
| 140 | |
| 141 | ---- |
| 142 | See also: TracTickets, TracIni |