Jump to content

How to change the name of a custom field?


neww

Recommended Posts

Hi everyone,

 

Best wishes for the New Year!

 

 

I have made a new custom field under Admin>Setup>Custom Client Fields.

In the Field Name, I have filled "tempUsername | Temp Username".

 

Now in the client area, in my custom PHP page, I want to access that field by the name. I was expecting that I could somehow access it by its name that I had given i.e. "tempUsername". Unfortunately I can only access it as "customfield3".

 

Is there a way that I can change the name from 'customfield3' to 'tempUsername'?

OR

Is there a way that I access that field by 'tempUsername'.

 

Thanks in advance!

Link to comment
Share on other sites

Is there a way that I can change the name from 'customfield3' to 'tempUsername'?

customfield3 is just an internal reference in the array - you wouldn't want to change that.

 

or Is there a way that I access that field by 'tempUsername'.

by default, no - you could easily loop through the $clientsdetails.customfields array and use the ID value - each ID value would be unique to that custom field... so in some ways better than a name, because you could create two client custom fields with the same name, but they'd have different ID values in the database.. so while you couldn't search for it specifically by name, you could by it's ID - in WHMCS, you'll find it useful to start referencing variable/arrays by ID rather than name for that reason (and others). :idea:

 

however, if you really *have* to access it by name, the only way would be to write an action hook to query the database tables and create a new array containing all the information you require. :roll:

 

you can create a new .php file in /includes/hooks, and then paste the code below into it - it will create a new $ccf array and make it available in the client area...

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function custom_client_fields_hook($vars) {

   $client = Menu::context('client'); 

   $ccf = Capsule::table('tblcustomfields')
           ->join('tblcustomfieldsvalues', 'tblcustomfields.id', '=', 'tblcustomfieldsvalues.fieldid')
           ->where('tblcustomfieldsvalues.relid',$client->id)
           ->where('type','client')
           ->where('adminonly','!=','on')    
           ->select('id','fieldname','fieldtype','description','value')
           ->get();

   $encodedata = json_encode($ccf);
   $decodedata = json_decode($encodedata, true);
   return array("ccf" => $decodedata);

}
add_hook("ClientAreaPage", 1, "custom_client_fields_hook");
?>

that will give you a new array, that you can then access in a foreach loop, e.g if you wanted to output the value of a specific custom field...

 

            {foreach $ccf as $clientfield}
               {if $clientfield.item eq "tempUsername | Temp Username"}
                   {clientfield.value}
               {/if}
           {/foreach}

and if you want to see what is contained within the $ccf array (or any other), add {debug} to the end of your template and it will generate a popup window... remember to remove it again when you've finished. :idea:

Link to comment
Share on other sites

customfield3 is just an internal reference....

...

...

...remove it again when you've finished. :idea:

 

brian! thank you very much for your clear and very helpful explanation! :D

And thank you for the sample code. It helps a lot for a noob like me :P

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated