How do can I get a field from the current user in an entry point?

Greetings,

I added a custom field to the Users and through research I found out that I can get that field by using something like:

<?php
global $current_user;
$fieldData = $current_user->my_field_c;

I tested this out on my local instance and it worked fine but when I installed the module in a online instance $fieldData always stays blank. Anyone knows the cause of the problem?

Thanks in advance,

André Policarpo

Parents
  • Hi André Policarpo

    Please check that its having value in that user array..

    check by below code . and custom field is set or not..

    global $current_user;
    echo "<pre>";
    print_r($current_user);
    echo "</pre>";

  • That should work - are you sure my_field_c is a field in the online instance (seeing as it is a custom field). Are you sure it's filled in for your user online?   

  • In entry points, if your custom field does not populate in the $current_user bean,  you should be able to use the BeanFactory to retrieve the full bean.

    $user = BeanFactory::retrieveBean('Users', $current_user->id);

    Should you need it in a controller I found the most efficient way is to add your custom field to the API that it is included in the app.user.

    The Current User API only pulls certain fields into the user data array.

    In the example below I populate support_group_c and business_unit_c which are custom fields on the users module.

    in:  <mysugar>/custom/clients/base/api/CustomCurrentUserApi.php

    <?php
    require_once 'clients/base/api/CurrentUserApi.php';
    class CustomCurrentUserApi extends CurrentUserApi
    {
        /**
         * Override the core api with our own version of retrieveCurrentUser
         */

        public function registerApiRest()
        {
            return array(
                'retrieve' => array(
                    'reqType' => 'GET',
                    'path' => array('me',),
                    'pathVars' => array(),
                    'method' => 'retrieveCurrentUser',
                    'shortHelp' => 'CustomWR: Returns current user info including custom info',
                    'longHelp' => 'include/api/help/me_get_help.html',
                    'ignoreMetaHash' => true,
                    'ignoreSystemStatusError' => true,
                ),
            );
        }

        /**
         * Extends retrieveCurrentUser and add our custom data
         */

        public function retrieveCurrentUser($api, $args)
        {
            $user_data_array = parent::retrieveCurrentUser($api, $args);

            global $current_user;
            $user_data_array['current_user']['support_group_c'] = $current_user->support_group_c;
            $user_data_array['current_user']['business_unit_c'] = $current_user->business_unit_c;

            return $user_data_array;
        }
    }
    ?>

    I can now use those fields in the controller, for example to get the support group:

    app.user.get('support_group_c');

    HTH

    FrancescaS

Reply
  • In entry points, if your custom field does not populate in the $current_user bean,  you should be able to use the BeanFactory to retrieve the full bean.

    $user = BeanFactory::retrieveBean('Users', $current_user->id);

    Should you need it in a controller I found the most efficient way is to add your custom field to the API that it is included in the app.user.

    The Current User API only pulls certain fields into the user data array.

    In the example below I populate support_group_c and business_unit_c which are custom fields on the users module.

    in:  <mysugar>/custom/clients/base/api/CustomCurrentUserApi.php

    <?php
    require_once 'clients/base/api/CurrentUserApi.php';
    class CustomCurrentUserApi extends CurrentUserApi
    {
        /**
         * Override the core api with our own version of retrieveCurrentUser
         */

        public function registerApiRest()
        {
            return array(
                'retrieve' => array(
                    'reqType' => 'GET',
                    'path' => array('me',),
                    'pathVars' => array(),
                    'method' => 'retrieveCurrentUser',
                    'shortHelp' => 'CustomWR: Returns current user info including custom info',
                    'longHelp' => 'include/api/help/me_get_help.html',
                    'ignoreMetaHash' => true,
                    'ignoreSystemStatusError' => true,
                ),
            );
        }

        /**
         * Extends retrieveCurrentUser and add our custom data
         */

        public function retrieveCurrentUser($api, $args)
        {
            $user_data_array = parent::retrieveCurrentUser($api, $args);

            global $current_user;
            $user_data_array['current_user']['support_group_c'] = $current_user->support_group_c;
            $user_data_array['current_user']['business_unit_c'] = $current_user->business_unit_c;

            return $user_data_array;
        }
    }
    ?>

    I can now use those fields in the controller, for example to get the support group:

    app.user.get('support_group_c');

    HTH

    FrancescaS

Children