How do I get current user id from .hbs files?

Hi all,

I'd like to get current user id from detail.hbs file in clients/base/fields/phone then I pass parameter to function one()

then function redirect to custom view.php like this: view.php?module=<module name>&id=<id>&user=

in detail.hbs:

{{#if value}}
<div class="ellipsis_inline" data-placement="bottom"{{#if dir}} dir="{{dir}}"{{/if}} title="{{value}}">
    {{#if skypeValue}}
        <a href="callto:{{skypeValue}}">{{value}}</a>
        <img src="custom/icon_phone.png" onclick="one('{{module}}','{{model.id}}','{{user.id}}');">
    {{else}}{{value}}{{/if}}
</div>
{{/if}}

for {{module}} return module name

for {{model.id}} return id

but for {{user.id}} not return any data.

I just tried with code below but not work:

...
<img src="custom/icon_phone.png" onclick="one('{{module}}','{{model.id}}','{{app.user.id}}');">
...

So for {{app.user.id}} and {{user.id}} not work.

How do I pass current user id into my function via detail.hbs or I can't?

Sugar 7.6 Ent

Thanks,

M

Parents
  • Hi Autchara,

    Simplest option to reach user data, you would add your user variable into your scope in your js file.

    ({
      user_id: "NOTINIT",
      initialize: function(options) {
        this.user_id = app.user.id;
        this._super('initialize', [options]);
      },
    })
    

    And print user_id in the hbs files like

    user_id: {{user_id}}
    

    Best Regards

    Tevfik Tümer

    Developer Support Engineer

  • Hi all,

    I found the solution.

    Thank you for advice from Tevfik Tümer

    Solution:

    After copy phone.js from clients/base/fields/phone/  and put into /custom/clients/base/fields/phone/

    modified phone.js like this:

    ({
            user_id: "NOTINIT",        //init parameter
            plugins:['EllipsisInline'],direction:'ltr',
            initialize:function(options){
                    var serverInfo=app.metadata.getServerInfo();
                    this.user_id = app.user.id;       //get user id
                    this.skypeEnabled=serverInfo.system_skypeout_on?true:false;
                    this._super('initialize',[options]);
            },
            format:function(value)
    ...
    

    add 2 lines of code in phone.js then display in detail.hbs

    ...  
    <img src="custom/icon_phone.png" onclick="one('{{module}}','{{model.id}}','{{user_id}}');">
    ...
    

    M

  • Hi Autchara,

    For sure this way would work but  I wouldn't recommend this solution. If you install an hotfix or upgrade your instance, you would override always with these customisation lines which is not a good practice.

    I just forgot to add "extendsFrom" key in my previous code block.

    Here is the correct code block;

    /*
     * Your installation or use of this SugarCRM file is subject to the applicable
     * terms available at
     * support.sugarcrm.com/.../.
     * If you do not agree to all of the applicable terms or do not have the
     * authority to bind the entity as an authorized representative, then do not
     * install or use this SugarCRM file.
     *
     * Copyright (C) SugarCRM Inc. All rights reserved.
     */
    ({
        extendsFrom: "BasePhoneField",
        user_id: "NOTINIT",
        initialize: function(options) {
            this.user_id = app.user.id;
            this._super('initialize', [options]);
        },
    })
    

    Best Regards

    Tevfik Tümer

    Developer Support Engineer

  • Thank for you advice I will concern with new incoming version.

    But I can't use the code above not work for me

  • Hi Autchara,

    Better to clarify, it's your customisations. I must show the correct way for other people who are seeking this solution. So, how you want to customise is all up to you.

    I've tested the code above and it's working for me.

    Did you run Quick Repair and Rebuild after making the changes?

    Best Regards

    Tevfik Tümer

    Developer Support Engineer

Reply Children
No Data