简体   繁体   English

Joomla-用户表中的更新名称

[英]Joomla - Update Name in the Users table

I have a Joomla 3 installed in my local machine. 我在本地计算机上安装了Joomla 3。

I've edited the default registration page to hide the Name field, wherein instead of using the full name as a field, I've divided it into First Name, Middle Name and Last Name. 我已经编辑了默认的注册页面以隐藏“名称”字段,其中我没有将全名用作字段,而是将其分为“名字”,“中间名”和“姓氏”。

Fortunately, the records being saved is successful and I can update them too. 幸运的是,保存的记录成功了,我也可以更新它们。

The problem is when the administrator starts managing the list of users, there's no link to proceed. 问题是当管理员开始管理用户列表时,没有链接可以继续。 This means the field with the link, which is the Name field (the full one) is blank. 这意味着带有链接的字段,即“名称”字段(完整字段)为空白。

My objective is that upon registration, I can fill that field out with a concatenated first, middle and last name. 我的目标是在注册后,可以用串联的名字,中间名和姓氏填写该字段。

How can I do this in Joomla 3? 如何在Joomla 3中做到这一点?

Thanks. 谢谢。

You can manage/manipulate user data using a User Plugin . 您可以使用“ 用户插件”管理/操作用户数据。 This link will show you how to use example.php (which, I believe, is included with the Joomla installation). 该链接将向您展示如何使用example.php (我相信Joomla安装中已包含了example.php )。 You're probably most interested in function onUserBeforeSave , and may not even need your module changes if you manipulate their input before Joomla saves it. 您可能对function onUserBeforeSave最感兴趣,并且如果在Joomla保存之前修改它们的输入,甚至可能不需要更改模块。

You would need to develop a custom user plugin to manage this properly. 您将需要开发一个自定义用户插件来正确管理此插件。 In addition, if you are going to write the plugin you should go ahead incorporate the entire use case into. 另外,如果您要编写插件,则应将整个用例纳入其中。 Below is a quick draft of how your main plugin file would look: 以下是主插件文件外观的快速草稿:

<?php
defined('_JEXEC') or die;

class plgUserCustom extends JPlugin
{
    public function __construct(& $subject, $config) 
    {
        parent::__construct($subject, $config);
    }

    // Called while Joomla is loading the form for the view
    // This is where you would remove or alter fields
    // and load your custom xml form definition for the 
    // first, middle and last names
    public function onContentPrepareForm($form, $data)
    {

        // Confirm valid JForm object or exit
        if (!($form instanceof JForm))
        {
            $this->_subject->setError('JERROR_NOT_A_FORM');
            return false;
        }

        // Check we are manipulating a valid form.
        // If the active view isn't listed in array exit as plugin not needed
        //
        // com_users.registration = front end registration form
        // com_users.profile = front end edit profile form
        // com_users.user = back end administrators form
        if (!in_array($form->getName(), array('com_users.registration', 'com_users.profile', 'com_users.user'))) {
            return true;
        }

        // The core JForm object for the view will be loaded
        // manipulate the fields any way you like
        // In below example, the name field will be removed
        // from the users registration, profile and admin user manager view
        if (in_array($form->getName(), array('com_users.registration', 'com.users.profile', 'com_users.user'))) {
            $form->removeField('name');

            // Now add the form xml path for your custom name fields
            // In this example the plugin has a forms directory
            JForm::addFormPath(dirname(__FILE__) . '/forms');
            $form->loadFile("custom", false);
        }

        return true;
    }

    // This is where Joomla loads any existing data into the form field
    // and where you would take the standard name field and separate
    // into first, middle and last name values and attach to data object.
    // The data object property names must match the field name in your
    // xml form definition file.
    public function onContentPrepareData($context, $data) {

        // Verify you are in the correct form before manipulating data
        if (!in_array($context, array('com_users.profile','com_users.user', 'com_users.registration'))) {
            return true;
        }


        // explode core name field into parts, attach to core data object
        // as first, middle and last name and unset name data.  
        // CAUTION:  You should implement check to verify middle name entered
        // or not by verifying if parts array length is 2 or 3

        $parts = explode(" ", $data->name);
        $data->first_name = $parts[0];
        $data->middle_name = $parts[1];
        $data->last_name = $parts[2];

        unset($data->name);

        }

        return true;
    }

    // This method fires before Joomla save the user data and is where
    // you should combine the first, middle and last names into one name
    // and attach to data object and remove references to first, middle and
    // last names.
    public function onUserBeforeSave($user, $isNew, $data) {

          // Manicure data before save, implode first, middle and last name 
          // into one field and add to data object, unset first, middle and last
          // name from data object
          $data->name = implode(" ", array($data->first_name, $data->middle_name, $data->last_name));
          unset($data->first_name);
          unset($data->middle_name);
          unset($data->last_name);


         return true;       
    }

    public function onUserAfterSave($data, $isNew, $result, $error) 
    {
         return true;
    }

    public function onUserAfterDelete($user, $succes, $msg)
    {
        return true;
    }

    public function onUserLogin($user, $options) 
    {

        return true;
    }

    public function onUserLogout($credentials, $options) {

        return true;
    }
}

I added links to writing a plugin to help you in creating and packaging the installation ZIP to install the plugin. 我添加了编写插件的链接,以帮助您创建和打包安装ZIP来安装插件。 I also attached a link for creating xml form definitions. 我还附加了用于创建xml表单定义的链接。

Good Luck! 祝好运!

http://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla http://docs.joomla.org/XML_JForm_form_definitions http://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla http://docs.joomla.org/XML_JForm_form_definitions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM