Restricting new profile field

I'm using elgg 2.3.14 and need assistance hiding a new profile field from view.  In my plugin's start.php I have registered a custom profile fields handler in the init like: 

elgg_register_plugin_hook_handler('profile:fields', 'profile', 'my_custom_profile_fields_handler');

My custom handler looks like:

function my_custom_profile_fields_handler($hook, $type, $return_value, $params) {

    // Types of fields: 'text', 'longtext', 'tags', 'url', 'email', 'location', 'date'

    // Add to language file(s) strings for the field label(s) like 'profile:my_first_custom_profile_field' => "

Label for my first custom field",

    $return_value['authId'] = 'text';

    return $return_value;

}

So I've added the authId profile field and now I want to display the value of this field on the user profile page, but I only want to show it if the current user is looking at his/her own profile page.  If they are viewing any other user's profile page, this field should be hidden.  I've tried following the trail of finding the view that displays this information by looking at /var/www/html/vendor/elgg/elgg/mod/profiles/views/default/profile/details.php and making some modifications to this file just to see if I notice any changes on the profile page, but none of my changes seem to be getting captured on the display... even after flushing the caches.  So my question is.. is this the correct view that I would want to override to put in my condition to check to see if the current user is viewing his own profile?

Thanks in advance.

  • if (!elgg_is_logged_in()) {
        return;
    }
    
    $user = $params['entity'];
    
    if (!($user instanceof \ElggUser) || !$user->canEdit()) {
        return;
    }

    Add this before your fields in the mentioned above code.

  • Thanks.. I will give that a try.

  • So I added the code to my custom_profile_fields_handler function right before $return_value['authId'] = 'text';

    It always ends up returning before reaching the authId field.  Seems as though if (!($user instanceof \ElggUser)) condition is always the case.  Still troubleshooting

  • You should add this code after

    function my_custom_profile_fields_handler($hook, $type, $return_value, $params) {

  • Right.. that's what I did.. here is my function as it stands now.. It seem as though the $user is null?

    function my_custom_profile_fields_handler($hook, $type, $return_value, $params) {
        // Types of fields: 'text', 'longtext', 'tags', 'url', 'email', 'location', 'date'
        
        if (!elgg_is_logged_in()) {
            return;
        }
        $user = $params['entity'];
        //system_message("user is: " . $user->guid);
        if (!($user instanceof \ElggUser)|| !$user->canEdit()) {
            system_message("either not ElggUser or can't edit");
            return;
        }
        $return_value['authId'] = 'text';
        return $return_value;
    }
  • Ok.

    This must work:

    $user = elgg_get_logged_in_user_entity();

    instead of

    $user = $params['entity'];
  • Sorry for not replying.. I had some family health distractions over the past couple of weeks.  So I tried using

    $user = elgg_get_logged_in_user_entity();

    but still get the same behavior.. as in when I visit another user's profile page besides my own the authId value still appears. 

  • function my_custom_profile_fields_handler($hook, $type, $return_value, $params) {
        if (!elgg_is_logged_in()) {
            return;
        }
        $user1 = $params['entity'];
        $user2 = elgg_get_logged_in_user_entity();
        if (!($user1 instanceof \ElggUser)|| $user1->guid != $user2->guid) {
            system_message("either not ElggUser or don't have view access");
            return;
        }
        $return_value['authId'] = 'text';
        return $return_value;
    }