Ocultar campo en editviewsdef en función de un valor del CurrentUser

Buenas.

Tengo un listener en un js

document.addEventListener( 'DOMContentLoaded', function() {
   showHidePanelContacts();
});

Como podría pasarle un valor del CurrentUser para ocultar un campo de edirviewdet?

O como se haría sin el listener. El listener lo tengo para ocultar ciertos paneles en el detalviewdefs en función de ciertos valores de campos.

Un saludo.

Parents
  • Hi Oscar 

                Do you want to show or hide contacts sub panel based on current user or what? thoda english mein samjovo bhai...

    Regards

    Sidhu

  • Hola Óscar del Río,

    Te contesto en inglés, así el resto de la comunidad puede entender mejor la respuesta.

    I would recommend you doing this through an after_ui_frame LogicHook triggered on the module you want to make the customization.

    The code of the LogicHook would be like the following:

    class LoadUserInEditView {

       function loadUserRolesInEditView( $bean, $event, $arguments ) {

           if( !$bean ) {
             return;
           }

           global $current_user;

           $department = $current_user->department;

           $departmentHtml = '<input type="hidden" style="display: none" id="user_department">' . $department . '</input>';

           echo $departmentHtml;

       }

    }

    This would set in the DOM a hidden input element which will store the department of the current user.

    Then in your javascript:

    document.addEventListener( 'DOMContentLoaded', function() {
       showHidePanelContacts();

       var department = $('#user_department').html();

       if( department !== 'my_desired_department' ) {
          $('#field_to_hide').hide();
       }
      
    });

    Espero que te sirva. Cualquier duda me dices!

    Saludos/Regards,

    David.

Reply
  • Hola Óscar del Río,

    Te contesto en inglés, así el resto de la comunidad puede entender mejor la respuesta.

    I would recommend you doing this through an after_ui_frame LogicHook triggered on the module you want to make the customization.

    The code of the LogicHook would be like the following:

    class LoadUserInEditView {

       function loadUserRolesInEditView( $bean, $event, $arguments ) {

           if( !$bean ) {
             return;
           }

           global $current_user;

           $department = $current_user->department;

           $departmentHtml = '<input type="hidden" style="display: none" id="user_department">' . $department . '</input>';

           echo $departmentHtml;

       }

    }

    This would set in the DOM a hidden input element which will store the department of the current user.

    Then in your javascript:

    document.addEventListener( 'DOMContentLoaded', function() {
       showHidePanelContacts();

       var department = $('#user_department').html();

       if( department !== 'my_desired_department' ) {
          $('#field_to_hide').hide();
       }
      
    });

    Espero que te sirva. Cualquier duda me dices!

    Saludos/Regards,

    David.

Children
  • Why not use field level permissions based on Roles?

    https://support.sugarcrm.com/Knowledge_Base/Users_Teams_Roles/Making_a_Field_Read-Only/ 

    Set field level permission to None for those Roles that should not see it.

    HTH

    FrancescaS

  • Hello Óscar del Río,

    As Francesca Shiekh mentioned above, if you can work with Roles, then you should be able to set this field hidden using the field level permissions.

    So, you should add all the users that DON'T have the Department = "Value1" to a role. Within that role, access to the Contacts fields ( Role settings  ) and set your field to None. That would hide this field for all the users on this role while all other users ( Department = "Value1" ) would be able to see it.

    I hope it helps but please let me know if you have any questions.

    Regards,
    Francesc del Moral
    Technical Support Engineer

  • Hello David.

    Your solution isn´t working for me. The department in js return null.

    I can´t use the another solutions commented because i'm working with de community edition, and i can't associate the visibility of fields to one role determinate.

    file.js

    document.addEventListener( 'DOMContentLoaded', function() {
       var department = $('#user_department').html();

       if( department !== 'Central' ) {
          $('#otras_observaciones_c').hide();
        }
          
       showHidePanelContacts();
    });

    function showHidePanelContacts() {
    ...

        

    hook.php

    <?php

    class Contacts {

       function verObservaciones( $bean, $event, $arguments ) {
           if( !$bean ) {
             return;
           }

           global $current_user;

           $department = $current_user->department;

           $departmentHtml = '<input type="hidden" style="display: none" id="user_department" name="user_department" >' . $department . '</input>'

           echo $departmentHtml;

       }

    }

    logic_hook.php

    ...

    $hook_array['after_ui_frame'][] = Array(1, 'Ver Observaciones', 'custom/modules/Contacts/Hooks/ContactsHook.php','Contacts', 'verObservaciones');

    ...

    Any idea why department return null?

    Thanks a lot.