Joomla - 更新Users表中的名称

时间:2014-09-04 08:18:33

标签: php joomla

我在本地机器上安装了Joomla 3.

我已修改默认注册页面以隐藏名称字段,其中我将其分为名字,中间名和姓氏,而不是使用全名作为字段。

幸运的是,保存的记录是成功的,我也可以更新它们。

问题是当管理员开始管理用户列表时,没有链接可以继续。这意味着带有链接的字段,即名称字段(完整的字段)为空。

我的目标是,在注册后,我可以用连接的名字,中间名和姓氏填写该字段。

如何在Joomla 3中执行此操作?

感谢。

2 个答案:

答案 0 :(得分:0)

您可以使用User Plugin管理/操作用户数据。此链接将向您展示如何使用example.php(我相信,它包含在Joomla安装中)。您可能对function onUserBeforeSave最感兴趣,如果在Joomla保存之前操作输入,可能甚至不需要更改模块。

答案 1 :(得分:0)

您需要开发自定义用户插件才能正确管理。此外,如果您要编写插件,则应继续将整个用例合并到其中。以下是主插件文件外观的快速草稿:

<?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;
    }
}

我添加了编写插件的链接,以帮助您创建和打包安装ZIP以安装插件。我还附上了一个用于创建xml表单定义的链接。

祝你好运!

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