| 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 | * format: One of: |
| 21 | * `plain` for plain text |
| 22 | * `wiki` to interpret the content as WikiFormatting |
| 23 | * `reference` to treat the content as a queryable value (''since 1.0'') |
| 24 | * `list` to interpret the content as a list of queryable values, separated by whitespace (''since 1.0'') |
| 25 | * '''checkbox''': A boolean value check box. |
| 26 | * label: Descriptive label. |
| 27 | * value: Default value (0 or 1). |
| 28 | * order: Sort order placement. |
| 29 | * '''select''': Drop-down select box. Uses a list of values. |
| 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 | * '''radio''': Radio buttons. Essentially the same as '''select'''. |
| 35 | * label: Descriptive label. |
| 36 | * options: List of values, separated by '''|''' (vertical pipe). |
| 37 | * value: Default value (one of the values from options). |
| 38 | * order: Sort order placement. |
| 39 | * '''textarea''': Multi-line text area. |
| 40 | * label: Descriptive label. |
| 41 | * value: Default text. |
| 42 | * cols: Width in columns |
| 43 | * rows: Height in lines. |
| 44 | * order: Sort order placement. |
| 45 | * format: Either `plain` for plain text or `wiki` to interpret the content as WikiFormatting. |
| 46 | |
| 47 | Macros will be expanded when rendering `textarea` fields with format `wiki`, but not when rendering `text` fields with format `wiki`. |
| 48 | |
| 49 | === Sample Config === |
| 50 | {{{ |
| 51 | [ticket-custom] |
| 52 | |
| 53 | test_one = text |
| 54 | test_one.label = Just a text box |
| 55 | |
| 56 | test_two = text |
| 57 | test_two.label = Another text-box |
| 58 | test_two.value = Default [mailto:[email protected] owner] |
| 59 | test_two.format = wiki |
| 60 | |
| 61 | test_three = checkbox |
| 62 | test_three.label = Some checkbox |
| 63 | test_three.value = 1 |
| 64 | |
| 65 | test_four = select |
| 66 | test_four.label = My selectbox |
| 67 | test_four.options = one|two|third option|four |
| 68 | test_four.value = two |
| 69 | |
| 70 | test_five = radio |
| 71 | test_five.label = Radio buttons are fun |
| 72 | test_five.options = uno|dos|tres|cuatro|cinco |
| 73 | test_five.value = dos |
| 74 | |
| 75 | test_six = textarea |
| 76 | test_six.label = This is a large textarea |
| 77 | test_six.value = Default text |
| 78 | test_six.cols = 60 |
| 79 | test_six.rows = 30 |
| 80 | }}} |
| 81 | |
| 82 | ''Note: To make entering an option for a `select` type field optional, specify a leading `|` in the `fieldname.options` option.'' |
| 83 | |
| 84 | === Reports Involving Custom Fields === |
| 85 | |
| 86 | 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`. |
| 87 | |
| 88 | {{{ |
| 89 | #!sql |
| 90 | SELECT p.value AS __color__, |
| 91 | id AS ticket, summary, owner, c.value AS progress |
| 92 | FROM ticket t, enum p, ticket_custom c |
| 93 | WHERE status IN ('assigned') AND t.id = c.ticket AND c.name = 'progress' |
| 94 | AND p.name = t.priority AND p.type = 'priority' |
| 95 | ORDER BY p.value |
| 96 | }}} |
| 97 | '''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. |
| 98 | |
| 99 | 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. |
| 100 | {{{ |
| 101 | #!sql |
| 102 | SELECT p.value AS __color__, |
| 103 | id AS ticket, summary, component, version, milestone, severity, |
| 104 | (CASE status WHEN 'assigned' THEN owner||' *' ELSE owner END) AS owner, |
| 105 | time AS created, |
| 106 | changetime AS _changetime, description AS _description, |
| 107 | reporter AS _reporter, |
| 108 | (CASE WHEN c.value = '0' THEN 'None' ELSE c.value END) AS progress |
| 109 | FROM ticket t |
| 110 | LEFT OUTER JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'progress') |
| 111 | JOIN enum p ON p.name = t.priority AND p.type='priority' |
| 112 | WHERE status IN ('new', 'assigned', 'reopened') |
| 113 | ORDER BY p.value, milestone, severity, time |
| 114 | }}} |
| 115 | |
| 116 | Note in particular the `LEFT OUTER JOIN` statement here. |
| 117 | |
| 118 | Note that if your config file uses an uppercase name, e.g., |
| 119 | {{{ |
| 120 | [ticket-custom] |
| 121 | |
| 122 | Progress_Type = text |
| 123 | }}} |
| 124 | you would use lowercase in the SQL: `AND c.name = 'progress_type'` |
| 125 | |
| 126 | === Updating the database === |
| 127 | |
| 128 | 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: |
| 129 | |
| 130 | {{{ |
| 131 | #!sql |
| 132 | INSERT INTO ticket_custom |
| 133 | (ticket, name, value) |
| 134 | SELECT |
| 135 | id AS ticket, |
| 136 | 'request_source' AS name, |
| 137 | 'None' AS value |
| 138 | FROM ticket |
| 139 | WHERE id NOT IN ( |
| 140 | SELECT ticket FROM ticket_custom |
| 141 | ); |
| 142 | }}} |
| 143 | |
| 144 | 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: |
| 145 | |
| 146 | {{{ |
| 147 | #!sql |
| 148 | INSERT INTO ticket_custom |
| 149 | (ticket, name, value) |
| 150 | SELECT |
| 151 | id AS ticket, |
| 152 | 'request_source' AS name, |
| 153 | 'None' AS value |
| 154 | FROM ticket |
| 155 | WHERE id NOT IN ( |
| 156 | SELECT ticket FROM ticket_custom WHERE name = 'request_source' |
| 157 | ); |
| 158 | }}} |
| 159 | |
| 160 | ---- |
| 161 | See also: TracTickets, TracIni |