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.

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

  • Hi Óscar del Río,

    Of course that's my fault.

    An input HTML element can't have HTML inside itself.

    Replace this:

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

    with this:

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

    You can use a paragraph (p), a label (label), a title (h1, h2) or whatever accepts including HTML inside.

    Maybe with the input would work like this:

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

    Just make some tests until it works.

    Hope it works.

    Regards,

    David.

  • No David, it´s my fault.

    It works fine with

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

    The js was already included in the view`s metadata, thak you for the information Francesca.

    Thanks a lot for sharing your knowledge.

Reply Children
No Data