簡體   English   中英

Joomla-用戶表中的更新名稱

[英]Joomla - Update Name in the Users table

我在本地計算機上安裝了Joomla 3。

我已經編輯了默認的注冊頁面以隱藏“名稱”字段,其中我沒有將全名用作字段,而是將其分為“名字”,“中間名”和“姓氏”。

幸運的是,保存的記錄成功了,我也可以更新它們。

問題是當管理員開始管理用戶列表時,沒有鏈接可以繼續。 這意味着帶有鏈接的字段,即“名稱”字段(完整字段)為空白。

我的目標是在注冊后,可以用串聯的名字,中間名和姓氏填寫該字段。

如何在Joomla 3中做到這一點?

謝謝。

您可以使用“ 用戶插件”管理/操作用戶數據。 該鏈接將向您展示如何使用example.php (我相信Joomla安裝中已包含了example.php )。 您可能對function onUserBeforeSave最感興趣,並且如果在Joomla保存之前修改它們的輸入,甚至可能不需要更改模塊。

您將需要開發一個自定義用戶插件來正確管理此插件。 另外,如果您要編寫插件,則應將整個用例納入其中。 以下是主插件文件外觀的快速草稿:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM