Add $current_user to PDF Templates on custom module


How can i add $current_user (the user that is currently logged in and generated the templated) to a PDF Template builded on a custom module?


I have similar issue at the moment. I know this is 4 year old post, but have You found any solution to this problem?

I don’t think you can get that without (paid) add-ons, such as my PowerReplacer.

I managed to get it working, by adding some code to the modules/AOS_PDF_Templates/templateParser.php file. It isn’t pretty, but it does the job it is suppose to do.

  1. At the beggining of the function parse_template_bean I added “global $current_user” like below

public function parse_template_bean($string, $key, &$focus)
global $app_strings, $sugar_config, $current_user;
$repl_arr = array();
$isValidator = new SuiteValidator();

  1. At the end of this function, right before the return statement i added these lines of code:

// added lines from current user
// user_name
$string = str_replace("{{{ current_user::user_name }}}", $current_user->user_name, $string);
// first_name
$string = str_replace("{{{ current_user::first_name }}}", $current_user->first_name, $string);
// last_name
$string = str_replace("{{{ current_user::last_name }}}", $current_user->last_name, $string);
// job_title
$string = str_replace("{{{ current_user::job_title }}}", $current_user->job_title, $string);
// phone_number
$string = str_replace("{{{ current_user::phone_number }}}", $current_user->phone_mobile, $string);
// email
$string = str_replace("{{{ current_user::email }}}", $current_user->email1, $string);

Now in PDF_Template by writing as an example {{{ current_user::user_name }}} I get currents user username and such. You can add more fields if you wish, I only needed these.

Im certain that @pgr plugin is better in every possible way, but this is all I needed.


That’s a very nice solution, thanks for sharing. :+1:

1 Like