HelloWorld - part 4 default values

From EPESI framework documentation
Jump to: navigation, search

In order to set default values for the fields you created you have to use the set_defaults method in your modules main file in the body function.

$this->rb->set_defaults(array('<field_name>'=>"<value>",'<field2_name>'=>"<value2>"));

Where rb is an instance of your recordset. You may set as many default values as you like. Remember to write the <field_name> in lowercase.

Lets say you wanted to set the default value of longtext to "some text" and set the select to your id. This is how the code should look like for mulitple defaults.

$me = CRM_ContactsCommon::get_my_record();
$this->rb->set_defaults(array('longtext'=>"some text",'select'=>$me["id"]));

CRM_ContactsCommon::get_my_record() returns an array of the logged in users recordset. Select fields are arranged by their index. If you want to set a default date the syntax is shown below (Y - year,m - month,d - day, H - hour,i - minute,s - seconds).

$this->rb->set_defaults(array('date'=>date('Y-m-d')));

The code above will output the current date. If besides the date you want to set the current time for timestamp the code should look like this.

$this->rb->set_defaults(array('timestamp'=>date('Y-m-d H:i:s')));

Below is an example of how it should look like.

 public function body() {

       $this->init_default_rb();

        $me = CRM_ContactsCommon::get_my_record();

        //default values
        $this->rb->set_defaults(array('longtext'=>"some text","select"=>$me['id']));
        $this->rb->set_defaults(array('date'=>date('Y-m-d')));
        $this->rb->set_defaults(array('timestamp'=>date('Y-m-d H:i:s')));


        $this->rb->set_header_properties(array('text'=>array('name'=>__('T'),'width'=>'35px')));

        $this->display_module($this->rb);


    }

The end result should look lik this where Admin is the user you are currently logged in as.

Default.png


Download: Get full tutorial here


NEXT >