Difference between revisions of "Adding Data to a Widget (Example)"
KenanHabib (talk | contribs) |
|||
Line 49: | Line 49: | ||
Third Step: | Third Step: | ||
− | A template - with the file | + | A template - with the file extension .tt2 - is responsible of view the data grabbed from the database on the the UI. Every widget in an object has its own separate template. So if you go to this directory root/templates/classes/gene_class/ which contains all the Gene class widgets you will find a template for the Overview widget with the name overview.tt2. |
+ | |||
+ | We are going to add a new WRAPPER for our field: | ||
+ | |||
+ | WRAPPER $field_block title="$title"||'Former Designating Laboratory' key="former_laboratory"; | ||
+ | tag2link(fields.former_laboratory.data.lab); '<br/>'; | ||
+ | fields.former_laboratory.data.time; | ||
+ | |||
+ | END; |
Revision as of 20:14, 13 June 2014
Introduction
A common form of feature request is add a piece of data to be displayed on a page.
Going through an issue example we are going to explain the work flow required to add some data to a widget.
Issue #2551 required showing the data under the "Former_designating_laboratory" field on the widget "Overview" of a "Gene class" (the Gene class "daf" for example).
Using the data flow graph (LINK) we can see that data goes through 4 stages from the database to the UI - Rest, Conf, API and Template. Usually our work doesn't involve the Rest stage, but it sure does involve modifying the .conf (the configuration), .pm (the API) and .tt2 (the template) files.
Let's go back to our example. If you go to the "daf" page (LINK), you can view the database you are going to be working on by going to Tools -> Tree Display using the menu on the left side of the page. Our field "Former_designating_laboratory" has two data objects under it, a time "19 Mar 2014 14:41:19" and a link to the lab "DR". In this example we are only going to care about displaying the time on the "Overview" widget (if you can't see a widget you can show it by clicking on it from the side menu).
First Step: Add a field in the configuration file. The wormbase.conf file, which you can find under the main website directory contains default configurations for the WormBase application. Now under the "overview" element of the "gene_class" section add the Former Designating Laboratory field "fields former_laboratory".
<gene_class> # <overview> name overview title Overview display report tooltip Gene class (eg Unc) overview. fields name fields other_names fields description fields laboratory fields former_laboratory # fields phenotypes fields remarks </overview>
Second Step: We are going to add a subroutine to the API of the Gene class object in order to grab the data from the database. You can find the API file we need to fix in lib/WormBase/API/Object/, then find the Gene_class object.
We are going to add a subroutine and call it former_laboratory:
sub former_laboratory {
my $self = shift; my $object = $self->object; # Access the field Former_designating_laboratory my $former_lab_time = $object->Former_designating_laboratory; return { description => 'Former_designating_laboratory', data => "$former_lab_time" || undef }; }
Return a hash that contains the description of the field and the former lab time. The value of the "data" key should be "undef" if there is no data in the "Former_designating_laboratory" field. If the "data" is "undef" the template won't show a "Former designating laboratory" field on the UI.
Third Step: A template - with the file extension .tt2 - is responsible of view the data grabbed from the database on the the UI. Every widget in an object has its own separate template. So if you go to this directory root/templates/classes/gene_class/ which contains all the Gene class widgets you will find a template for the Overview widget with the name overview.tt2.
We are going to add a new WRAPPER for our field:
WRAPPER $field_block title="$title"||'Former Designating Laboratory' key="former_laboratory";
tag2link(fields.former_laboratory.data.lab); '
'; fields.former_laboratory.data.time;
END;