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

  • 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

  • Thank you for helping me Tevfik Tümer

    I copy phone.js into /custom/clients/base/fields/phone/

    then modify by put you code on top of files like this:

    ({
    user_id: "NOTINIT",
      initialize: function(options) {
        this.user_id = app.user.id;
        this._super('initialize', [options]);
    },
    //next line is original code
    plugins:['EllipsisInline'],direction:'ltr'...
    

    but it return 'NOTINIT'

    How do I fix it or hierarchy is wrong ?

  • Hi Autchara,

    If you notice, I'm calling _super method. That means trying to call parent functionality which is you are duplicating .

     

    You don't actually clone the whole file into custom folder. Just create phone.js file under /custom/clients/base/fields/phone/ and use the code block I sent you.

    It should work as it's expected.

    Best Regards

    Tevfik Tümer

    Developer Support Engineer

  • I just created phone.js and put your code into file.

    It like using this files is a main point that make all functional in phone.js(original) has gone. (eg. skype call)

    If I copy phone.js it still work with skype call but user_id not shown.

  • 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

  • Yes, I do.

    But when I use your code, the phone number look like plain text cannot click like before.

    I cannot change correct answer.