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

  • First hide the form element (CSS is fine, make a myplugin.css view and extend elgg.css). Then you want to keep them in sync. Create a handler for the event update:after, user and have it set $user->name = $user->username and $user->save().

  • CSS: .elgg-form-usersettings-save .elgg-module:first-of-type { display:none; }

  • I think I am not quite understanding the event handler part.... My code crashes the whole site into a 500 error lol

    /mod/lockdisplayname/start.php

    <?php
    /*
     * Lock Display Name Plugin
     *
     */
     
    elgg_register_event_handler('init', 'system', 'lockdisplayname_init');
     
    function lockdisplayname_init() {
        elgg_extend_view('css/elgg', 'lockdisplayname/css');
    }

    function event_handler('update:after', 'user') {

    $user->name = $user->username and $user->save();

    }

  • I also tried this, it does not crash the site, but has no effect on hiding Display Name option..

    /mod/lockdisplayname/start.php

    <?php
    /*
     * Lock Display Name Plugin
     *
     */
     
    elgg_register_event_handler('init', 'system', 'lockdisplayname_init');
    elgg_register_event_handler('update:after', 'user', 'lockdisplayname_sync');
     
    function lockdisplayname_init() {
        elgg_extend_view('css/elgg', 'lockdisplayname/css');
    }

    function lockdisplayname_sync() {
        $user->name = $user->username and $user->save();
    }

  • /mod/lockdisplayname/views/default/lockdisplayname/css.php

     

    .elgg-form-usersettings-save .elgg-module:first-of-type { display:none; }

  • Any pointers on what I may not be grasping about this?

  • It's not

    $user->name = $user->username and $user->save();

    but

    $user->name = $user->username;
    
    $user->save();

    Not sure about the css changes and what they result in. Instead of "hiding" the form element it might also be an option to override the user settings form where the display name can be changed within your plugin with a form without the corresponding input field for the display name. Instead setting the display name input value within the form to be the same as the username of the page owner (= the user). I assume you have the account registering off as you say that you create all accounts yourself. Otherwise, you would also override the register form and handle the saving of the display name.

    You might also make sure that there are no issues with using the Profile Manager plugin. This plugin allows changing the username (if the corresponding plugin option would be enabled). Not sure if this functionality might cause issues - even if not used - if you don't want to allow changes in display name. If the Profle Manager plugin would override the core forms/views that handle the display name change any changes made in these views/form within your own plugin might not take effect (or the other way round: cause issues in the Profile Manager plugin) depending with plugin is below the other (and therefore overrides any views/forms already overriden by other plugins above).

  • At the core, all I want to do is take the form element off of the forms a user sees for Display Name... It can stay on the admin side.

    I don't need to do anything more than that.

    I do use the Profile Manager plugin, the change username option is disabled, and there is no option to disable Display Name.

    I don't need the code to save the username as the display name for me. There may be times I elect to use variations, for example, a prefix to show if a user is staff.

     

    I have much to learn on how Elgg works, and bigger tasks I want to tackle after this, like possibly enabling registration by means of personally giving someone a one time use code, in that instance, yeah, making the username save as both the username and the display name makes since in my case on the registration page only.

     

    But, before I try to go after anything that complicated, it is important I at least learn how to do something as trivial as disable a core form element.

    So, the best way of hiding that element from just the user, without otherwise changing functionality, is what approach?

  • You can override the view vendor/elgg/elgg/views/default/core/settings/account/name.php in your plugin by creating a modified version at mod/your_plugin/views/default/core/settings/account/name.php that looks like

    <?php
    
    /**
     * Provide a way of setting your full name.
     *
     * @package Elgg
     * @subpackage Core
     */
    $user = elgg_get_page_owner_entity();
    
    if (!$user instanceof ElggUser) {
        return;
    }
    
    if (elgg_is_admin_logged_in()) {
        $title = elgg_echo('user:name:label');
        $content .= elgg_view_field(array(
            '#type' => 'text',
            'name' => 'name',
            'value' => $user->name,
            '#label' => elgg_echo('name'),
        ));
    
        echo elgg_view_module('info', $title, $content);
    
    } else {
        echo elgg_view_field(array(
            '#type' => 'hidden',
            'name' => 'name',
            'value' => $user->name,
        ));
    }

    This will still allow an admin to change the display name (also of other users) but will hide the display name input field from the settings form. If a normal user saves the settings the current display name will just be kept unchanged (the hidden input field containing the display name as value is used to make it unnecessary to make changes in the save action).

    That's the simpliest way of modifications. But it will only prevent the modification of display name if the users can't register on their own in the first place as the registration form would still contain the display name input. If the users should be able to register on their own, more changes would be necessary (in the registration form and then also in the save action/account registration action - the trick with the hidden input won't work in this case as the account is not yet created).

    I don't think that you can totally drop the display name easily. It's used at many places in code explicitely not only in Elgg core but likely also in many 3rd party plugins.

  • Thank you so much for your answer.. My goal is not to drop usage of the Display Name, just have administrative oversight of setting it and preventing a user from changing it themselves.

     

    Do I need to do anything in start.php?

    <?php
    /*
     * Hide Display Name Plugin
     *
     */
     
    elgg_register_event_handler('init', 'system', 'hidedisplayname_init');
     
    function hidedisplayname_init() {
    }

    I gave it a shot and a user can still see the Display Name option in Edit Profile

    Thank you so much!

     

    Update:

     

    I see what this is doing :) It does hide the option for the user to change their Display Name in Settings, it is just view-able still in "Edit Profile"...

     

    So I guess my start.php is fine.

     

    I just need to find where the "edit Profile" page is coming from, and change that with a modified version in the proper directory of my plugin...

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