Changes between Version 1 and Version 2 of TracTicketsCustomFields


Ignore:
Timestamp:
Feb 13, 2021 12:24:55 AM (3 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracTicketsCustomFields

    v1 v2  
    1 = Custom Ticket Fields =
     1= Custom Ticket Fields
     2
    23Trac supports adding custom, user-defined fields to the ticket module. Using custom fields, you can add typed, site-specific properties to tickets.
    34
    4 == Configuration ==
     5== Configuration
     6
    57Configuring custom ticket fields is done in the [wiki:TracIni trac.ini] file. All field definitions should be under a section named `[ticket-custom]`.
    68
     
    1113 ...
    1214}}}
     15
    1316The example below should help to explain the syntax.
    1417
    15 === Available Field Types and Options ===
     18=== Available Field Types and Options
     19
    1620 * '''text''': A simple (one line) text field.
    1721   * label: Descriptive label.
    1822   * value: Default value.
    19    * order: Sort order placement. (Determines relative placement in forms.)
     23   * order: Sort order placement. Determines relative placement in forms with respect to other custom fields.
     24   * format: One of:
     25     * `plain` for plain text
     26     * `wiki` to interpret the content as WikiFormatting
     27     * `reference` to treat the content as a queryable value (''since 1.0'')
     28     * `list` to interpret the content as a list of queryable values, separated by whitespace (''since 1.0'')
    2029 * '''checkbox''': A boolean value check box.
    2130   * label: Descriptive label.
    22    * value: Default value (0 or 1).
     31   * value: Default value: 0 or 1.
    2332   * order: Sort order placement.
    2433 * '''select''': Drop-down select box. Uses a list of values.
    2534   * label: Descriptive label.
    2635   * options: List of values, separated by '''|''' (vertical pipe).
    27    * value: Default value (Item #, starting at 0).
     36   * value: Default value (one of the values from options).
    2837   * order: Sort order placement.
    2938 * '''radio''': Radio buttons. Essentially the same as '''select'''.
    3039   * label: Descriptive label.
    3140   * options: List of values, separated by '''|''' (vertical pipe).
    32    * value: Default value (Item #, starting at 0).
     41   * value: Default value (one of the values from options).
    3342   * order: Sort order placement.
    3443 * '''textarea''': Multi-line text area.
    3544   * label: Descriptive label.
    3645   * value: Default text.
    37    * cols: Width in columns.
     46   * cols: Width in columns
    3847   * rows: Height in lines.
    3948   * order: Sort order placement.
     49   * format: Either `plain` for plain text or `wiki` to interpret the content as WikiFormatting.
    4050
    41 === Sample Config ===
    42 {{{
     51Macros will be expanded when rendering `textarea` fields with format `wiki`, but not when rendering `text` fields with format `wiki`.
     52
     53=== Sample Configuration
     54
     55{{{#!ini
    4356[ticket-custom]
    4457
     
    4861test_two = text
    4962test_two.label = Another text-box
    50 test_two.value = Just a default value
     63test_two.value = Default [mailto:joe@nospam.com owner]
     64test_two.format = wiki
    5165
    5266test_three = checkbox
     
    5771test_four.label = My selectbox
    5872test_four.options = one|two|third option|four
    59 test_four.value = 2
     73test_four.value = two
    6074
    6175test_five = radio
    6276test_five.label = Radio buttons are fun
    6377test_five.options = uno|dos|tres|cuatro|cinco
    64 test_five.value = 1
     78test_five.value = dos
    6579
    6680test_six = textarea
     
    7185}}}
    7286
    73 ''Note: To make entering an option for a `select` type field optional, specify a leading `|` in the `fieldname.options` option.''
     87'''Note''': To make entering an option for a `select` type field optional, specify a leading `|` in the `fieldname.options` option.
    7488
    75 === Reports Involving Custom Fields ===
     89=== Reports Involving Custom Fields
    7690
    77 The SQL required for TracReports to include custom ticket fields is relatively hard to get right. You need a `JOIN` with the `ticket_custom` field for every custom field that should be involved.
     91Custom 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`.
    7892
    79 The following example includes a custom ticket field named `progress` in the report:
    80 {{{
    81 #!sql
     93{{{#!sql
     94SELECT p.value AS __color__,
     95   id AS ticket, summary, owner, c.value AS progress
     96  FROM ticket t, enum p, ticket_custom c
     97  WHERE status IN ('assigned') AND t.id = c.ticket AND c.name = 'progress'
     98AND p.name = t.priority AND p.type = 'priority'
     99  ORDER BY p.value
     100}}}
     101
     102'''Note''': 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 is all that is required, you're set.
     103
     104However, 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:
     105{{{#!sql
    82106SELECT p.value AS __color__,
    83107   id AS ticket, summary, component, version, milestone, severity,
     
    86110   changetime AS _changetime, description AS _description,
    87111   reporter AS _reporter,
    88   (CASE WHEN c.value = '0' THEN 'None' ELSE c.value END) AS progress
     112   (CASE WHEN c.value = '0' THEN 'None' ELSE c.value END) AS progress
    89113  FROM ticket t
    90114     LEFT OUTER JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'progress')
     
    96120Note in particular the `LEFT OUTER JOIN` statement here.
    97121
     122Note that if your config file uses an '''uppercase''' name:
     123{{{#!ini
     124[ticket-custom]
     125
     126Progress_Type = text
     127}}}
     128you would use '''lowercase''' in the SQL: `AND c.name = 'progress_type'`.
     129
     130=== Updating the database
     131
     132As noted above, any tickets created before a custom field has been defined will not have a value for that field. Here is some SQL (tested with SQLite) that you can run directly on the Trac database to set an initial value for custom ticket fields. It inserts the default value of 'None' into a custom field called 'request_source' for all tickets that have no existing value:
     133
     134{{{#!sql
     135INSERT INTO ticket_custom
     136   (ticket, name, value)
     137   SELECT
     138      id AS ticket,
     139      'request_source' AS name,
     140      'None' AS value
     141   FROM ticket
     142   WHERE id NOT IN (
     143      SELECT ticket FROM ticket_custom
     144   );
     145}}}
     146
     147If 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:
     148
     149{{{#!sql
     150INSERT INTO ticket_custom
     151   (ticket, name, value)
     152   SELECT
     153      id AS ticket,
     154      'request_source' AS name,
     155      'None' AS value
     156   FROM ticket
     157   WHERE id NOT IN (
     158      SELECT ticket FROM ticket_custom WHERE name = 'request_source'
     159   );
     160}}}
     161
    98162----
    99163See also: TracTickets, TracIni