5

I am following this example here Profile plugin

to create my own profile plugin. What I can't figure out is how to make a field admin only?

In the original customer / profile there are fields like language or default editor. They can be disabled via ' Frontend User Parameters' on/off.

How do I create such a field? Visible to ADMIN only, not to the user.

There are parameters like name="register-require_myfield", are their any to disable a field for registration, or profile?

I can't find anything about this problem online, what am I missing ?

Thanks

EDIT: This is Joomla 3.2+

2

I'm not dead sure that this will work as i don't have such a user plugin installed to try, but try adding this function to your plugin. It's intent is to check if the user is in a usergroup of your choosing, and if not, exit and not prepare the form.

public function onContentPrepareForm($form, $data)
{
    $groups = $data->groups->getProperties();
    if(!in_array("8",$groups)){    // see table #__usergroups for a complete list to check
        return;
    }
}

Upon further thought (and maybe I'll try it later) you may have to adjust the contents of $form using methods of jForm. Again, not sure if it will work, but seems like a good place to start. I would be interested to know if you resolve it to put it in my own little bag of tricks.

  • IC, so I have to manually 'SKIP' fields I don't want the user to see in the profile ... I'll give your idea a try, thnx – Andreas Dec 1 '14 at 21:03
  • you colud set them as hidden as well – GDP Dec 1 '14 at 21:35
  • I'm going to accept your answer as it did point me into the right direction. However, I did not end up using your suggestion. Since I also have a component going with this it turns out to be way simpler to use the plugin just to add the fields I need and create my own forms/views in the component using JUser class to do the CRUD. Rather simple once you figure out that this class handles custom profile fields as well. Thanx If I only had the plugin code, I would have used your suggestion. – Andreas Dec 3 '14 at 19:12
  • As long as you got 'er done...glad to have helped. – GDP Dec 3 '14 at 19:19
0

You can edit the Profile Plugin. Edit the onContentPrepareForm() change the profile case to something like this:

// Case profile in site         
        elseif ($name == 'com_users.profile')
        {
            // Toggle whether the field is required.
            if ($this->params->get('profile-require_' . $field, 1) > 0)
            {
                $form->setFieldAttribute($field, 'required', ($this->params->get('profile-require_' . $field) == 2) ? 'required' : '', 'profile');

                // Remove credits field from the front-end user profile edit page
                if($field == 'usercredits') {
                    $form->removeField('usercredits', 'profile');
                }
            }
            else
            {
            // Case profile in site         
        elseif ($name == 'com_users.profile')
        {
            // Toggle whether the field is required.
            if ($this->params->get('profile-require_' . $field, 1) > 0)
            {
                $form->setFieldAttribute($field, 'required', ($this->params->get('profile-require_' . $field) == 2) ? 'required' : '', 'profile');

                // Remove credits field from the front-end user profile edit page
                if($field == 'usercredits') {
                    $form->removeField('usercredits', 'profile');
                }
            }
            else
            {
                $form->removeField($field, 'profile');                  
                // Remove credits field from the front-end user profile edit page
                if($field == 'usercredits') {
                    $form->removeField('usercredits', 'profile');
                }
            }
  • Can you explain how your code ensures that only a site admin can see the field? Cause from what I'm seeing, this isn't what your code does – Lodder May 20 '15 at 10:49
  • The field is listed as optional in the edit profile plugin in the back end. It will be shown to the admin, to prevent it being shown to the user, it removed when the form is prepared to be displayed. $form->removeField does that manually for you. The user is unable to edit that field in the front end. – samaras May 20 '15 at 12:48
  • Interesting, I'll give it a try – Andreas Jun 7 '15 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.