ToggleCFs

From Request Tracker Wiki
Jump to: navigation, search

Toggle Custom Fields

The following only works with RT 4.x

We had a need to show/hide custom fields depending on the value chosen for a certain "control" field. That is, if the control field had a certain value, we wanted to show a certain set of custom fields for editing and display. If the control field did not have that value, we wanted to hide those custom fields entirely.

This could probably have been done easier in Javascript, but nobody on our team knows Javascript.

Note that the following code is for

local/html/Callbacks/MyCallbacks/Elements/ShowCustomFields/MassageCustomFields

And the callback at (not the same path as above!):

local/html/Callbacks/MyCallbacks/Ticket/Elements/EditCustomFields/MassageCustomFields

is nearly identical to this code below. You'll need both callbacks in place to hide/show CFs while on the Modify.html and on the Display.html page. No reason to paste code twice.

%# local/html/Callbacks/MyCallbacks/Elements/ShowCustomFields/MassageCustomFields

%#
%# If ticket is not "Noteworthy" do not display a certain
%# set of custom fields to the user.
%#

<%args>

$CustomFields => undef
$ARGS => undef
$Object => undef
</%args>

<%init>

my $ticket = $Object;
my $showchildren = 1;
# We're calling these togglable fields "child" fields
# Don't make too much of the nomenclature.
my @childfields = ( "Child1",
                    "Child2",
                    "MyChild",
                    "Noodle"
);

while (my $cf = $CustomFields->Next) {

    my $nam = $cf->Name;
    my $val = $ticket->FirstCustomFieldValue($nam);
    if ($nam =~ /^noteworthy$/i and $val !~ /^yes$/i) {
        $showchildren = 0;
        last;
    }
}

if (! $showchildren) {

    # "Remove" each child field from $CustomFields to not show it
    for (@childfields) {
        # "Limit" is part of DBIx-SearchBuilder (google it)
        $CustomFields->Limit(FIELD => 'Name', OPERATOR => '!=',
                             VALUE => "$_", ENTRYAGGREGATOR => 'AND',
                             CASESENSITIVE => 0);
    }
}
</%init>