Making "Display Name" a field only an admin can change

Greetings,

I have been trying to figure out where to begin making a plugin to hide the option to change a "Display Name" or to restrict it to only allowing an Admin to change it.

I run a private site, and in this scenario, I create the account for community members.

I wish to have an individual's "username" and "display name" be the same and remain as such.

I have used the Profile Manager plugin to add a new custom field for Full Name.

 

It is my goal to have a consistent user experience, and there are default behaviors that break this consistency in my point of view. For example, I want member lists to display the username of an individual, and I also want to make tagging easier for the end user.

 

In my scenario, since I am making all the accounts myself, it is an easier approach to just stop a member from changing the "display name" I set, or being able to see that option in their settings and edit profile.

 

I am new to Elgg, and the Elgg way of doing things, but I do understand it is preferred that such customizations are made as plugins instead of editing the core.

 

I have made a couple basic tutorial plugins, and am starting to grasp the idea, I just have no clue where I would start to hide these options, or make them restricted to an admin changing.

If I make a solid plugin, I would be happy to share it here with the community, as it seems others have wanted a similar option, however there was never a solid answer or a plugin made.

 

Thanks

  • Oops. I wasn't aware that you can change the display name also on the "edit profile" page. My suggestion was only for the user settings page.

    To hide the display name input field on the edit profile page you will also have to override the view vendor/elgg/elgg/views/default/forms/profile/edit.php in your plugin. The change to be made is for the first input field and it would have to be changed into

    if ($entity->isAdmin()) {
        echo elgg_view_field(array(
            '#type' => 'text',
            'name' => 'name',
            'value' => $entity->name,
            '#label' => elgg_echo('user:name:label'),
            'maxlength' => 50, // hard coded in /actions/profile/edit
        ));
    } else {
        echo elgg_view_field(array(
            '#type' => 'hidden',
            'name' => 'name',
            'value' => $entity->name,
        ));
    }

    Additionally, with the Profile Manager plugin enabled, the change above will not have an effect on the edit profile page, because this view is already overriden. Instead the Profile Manager plugin uses the view mod/profile_manager/views/default/profile/edit/name.php. So, you would have to override this view also in your plugin with the modified view

    <?php
    
    if ($vars['entity']->isAdmin()) {
        $content = elgg_format_element('label', [], elgg_echo('user:name:label'));
        $content .= elgg_view('input/text', ['name' => 'name', 'value' => $vars['entity']->name]);
    
        echo elgg_format_element('div', [], $content);
    } else {
        echo elgg_view('input/hidden', ['name' => 'name', 'value' => $vars['entity']->name]);
    }

    and place your plugin below the Profile Manager plugin in the plugin list.

  • P.S. no additional code in start.php necessary.

  • Thank you so very much! I think I got this now!

  • The plugin I have made from this I have added you as an author in the manifest... I would have not had a clue what to do without your help, and this has done a great deal to teach me how views work, and how dependencies relate. Thank you for all your help, and all you have taught me through this

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking