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

Reply
  • 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

Children
  • 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 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