SuiteCRM: Change Date Format in PDF Template

Category: SuiteCRM

The default date format within SuiteCRM depends on the locale set within the application. But if you want to customize the date format available within the PDF Template, use the following code and make changes as per your need for date formatting.

Warning: This change is not upgrade safe. If you upgrade SuiteCRM, you must make this change again.

Go to file ROOT/modules/AOS_PDF_Templates/templateParser.php

In function parse_template_bean change following.

public function parse_template_bean($string, $key, &$focus)
    {
        // added $current_user
        global $app_strings, $sugar_config, $current_user;
        $repl_arr = array();
        $isValidator = new SuiteValidator();
        // add these two lines
		$datef = $current_user->getPreference('datef');
		$timef = $current_user->getPreference('timef');

		foreach ($focus->field_defs as $field_def) {
            // replace single line "$repl_arr[$key . "_" . $fieldName] = $focus->{$fieldName};" in "else" block with following multiple lines
                    if(empty($focus->{$fieldName})) {
						$repl_arr[$key . "_" . $fieldName] = "";
					} else 
					if ($field_def['type'] == 'date') {
						$dt = DateTime::createFromFormat($datef, $focus->{$fieldName})->getTimestamp();
						$repl_arr[$key . "_" . $fieldName] = strval(date("j M, Y", $dt));
					} else if ($field_def['type'] == 'datetime')
					{
						$dt = DateTime::createFromFormat($datef . ' ' . $timef, $focus->{$fieldName})->getTimestamp();
						$repl_arr[$key . "_" . $fieldName] = strval(date("j M, Y", $dt));
					}
					else {
						$repl_arr[$key . "_" . $fieldName] = $focus->{$fieldName};
					}
        } // end foreach()

Share

0 comments

Your email address will not be published. Required fields are marked *