By Daniel Pierce on Wednesday, 30 August 2017
Posted in General
Replies 14
Likes 0
Views 514
Votes 0
I'm having a challenge getting setFieldValue to work for JOOMLA_FULLNAME for an user account. Here is the code that I'm using to test if I can create the custom field entry.


require_once ( JPATH_BASE . 'administrator' . DS . 'components' . DS . 'com_easysocial' . DS . 'includes' . DS . 'easysocial.php' );
require_once ( JPATH_BASE . 'administrator' . DS . 'components' . DS . 'com_easysocial' . DS . 'includes' . DS . 'user' . DS . 'user.php' );
require_once ( JPATH_BASE . 'media' . DS . 'com_easysocial' . DS . 'apps' . DS . 'fields' . DS . 'user' . DS . 'joomla_fullname' . DS . 'joomla_fullname.php' );


$user_id = (int) '658';

$user = ES::user($user_id);
$name = new SocialFieldUserJoomla_FullnameObject($user, NULL);

$name->first = (string) 'Fran';
$name->middle = (string) 'Fred';
$name->last = (string) 'Froster';
$name->name = (string) 'Fran Fred Froster';
echo ( '<br>');
var_dump($name);
echo ( '<br><br>');
echo ( $name->toJson() );

$user->setFieldValue('JOOMLA_FULLNAME', $name->toJson() );


I was able to get the setFieldValue to work for all the single fields as well as the address field, but the joomla_fullname just won't let me set the custom field value.

I have the latest version of Joomla and EasySocial installed on Ubuntu 16.04.

Any help would be appreciated.
Dan
Hi Daniel,

Actually we store the name fields as an object, not json data. As such you can use the codes as below for the storing part.

$user->setFieldValue('JOOMLA_FULLNAME', (object) $name);


// Initialize default registry
$registry = ES::registry();

foreach ($name as $key => $value) {
$registry->set($key, $value);
}

$data = $registry->toArray();
$user->bind($data);

$user->save();
We also added the bind and save section because we realize that without it, the name field in Joomla would not be updated(http://take.ms/614T1).
·
Wednesday, 30 August 2017 13:49
·
0 Likes
·
0 Votes
·
0 Comments
·
Raymond,

Thank you for your recommendation.

I added the code to my test script. The code sets the Users Full name in the $user->name field, but does not create the JOOMLA_FULLNAME record in the Social_Fields_Data table. I've attached screen shots of the mysql tables after running the script.

I expected there to be 4 new entries in the Social_Fields_Data table for the user_id = 658 with field_id = 2.
datakey = first data = Frank
datakey = middle data = F
datakey = last data = Froster
datakey = name data = Frank F Froster

I originally had a JOOMLA_FULLNAME (field_id=2) field in the record, so I deleted it thinking it may be an issue, but that didn't make a difference.

So I'm able to change the name in the $user->name field, but cannot create a Social_Fields_Data table entry.

Any thoughts on what might be causing the record not to be created?
Thanks
Dan
·
Thursday, 31 August 2017 01:48
·
0 Likes
·
0 Votes
·
0 Comments
·
Raymond,

Though I should add the output of my test script that does a setFieldValue and then a getFieldValue for JOOMLA_FULLNAME.

Here is what is displayed in the browser.
__________________________________________________________

Name Test


User ID = 658

object(SocialFieldUserJoomla_FullnameObject)#609 (5) { ["first"]=> string(5) "Frank" ["middle"]=> string(1) "F" ["last"]=> string(7) "Froster" ["name"]=> string(15) "Frank F Froster" ["format"]=> string(1) "1" }

Name Data Updated
Retreived Name
object(SocialFieldUserJoomla_FullnameObject)#2067 (5) { ["first"]=> string(15) "Frank F Froster" ["middle"]=> string(0) "" ["last"]=> string(0) "" ["name"]=> string(15) "Frank F Froster" ["format"]=> string(1) "1" }

First: Frank F Froster
Middle:
Last:
Name: Frank F Froster
_______________________________________________________________

As you see I'm sending the object to the setFieldValue with all the values set, but when I get the value back, they don't reflect the values in the original object. I then look at the database and the records have not been created in the Social_Fields_Data table.

Hope that helps.
Dan
·
Thursday, 31 August 2017 02:11
·
0 Likes
·
0 Votes
·
0 Comments
·
Hm, looking at the code it seems like EasySocial is lacking a "requireValidation" flag if inserting values through the API. Here's what you can do, download the attached file and upload it into /administrator/components/com_easysocial/includes/user/

Then, run your codes like this,

[gist]
// First we store these data into EasySocial fields table because this is the only way we can know what's the user's first, middle or last name

$data = array(
'first' => "First",
'middle' => "Middle",
'last' => "Last"
);

$user = ES::user();
$user->setFieldValue('JOOMLA_FULLNAME', $data, false);

// Once the mapping is stored in our table, we need to update Joomla's table
$user->name = $data['first'] . ' ' . $data['middle'] . ' ' . $data['last'];
$user->store();
[/gist]
·
Saturday, 02 September 2017 18:10
·
0 Likes
·
0 Votes
·
0 Comments
·
StackIdeas Support,

Sorry for the slow response. Wanted to let you know that the change to user.php corrected the issue and I'm now able to update the JOOMLA_USERNAME value through the API.

Please let me know what release this change will be included with.

Thank you so much for all the help.
Dan
·
Wednesday, 13 September 2017 02:12
·
0 Likes
·
0 Votes
·
0 Comments
·
Hey Dan,

You're most welcome.

This change will be included in EasySocial 2.1.

Regards.
·
Wednesday, 13 September 2017 11:40
·
0 Likes
·
0 Votes
·
0 Comments
·
Mark and Raymond,

Everything that we changed worked just great on my test server, as I reported above, that has:
Ubuntu V16.04
Joomla V3.7.4
ES V2.0.22
Contains User.php that you provided above

BUT when I take the exact same code to my production environment with:
Ubuntu V16.04
Joomla V3.7.5
ES V2.0.25
Contains User.php that you provided above

I get a Server 500 error until I comment out the following line. So the server doesn't like the store() method call all of a sudden.
$user->store();

Do I need to add some additional includes with the test script for the latest EasySocial?
Only thing that has changed is the version of ES and Joomla, so some definitions must have changed.

I've attached the test script software that I'm using.

Dan
·
Friday, 22 September 2017 05:37
·
0 Likes
·
0 Votes
·
0 Comments
·
You need to take a look at the error message that is being generated Dan. If you have disabled errors, it would be very difficult to know what are the errors.
·
Friday, 22 September 2017 15:18
·
0 Likes
·
0 Votes
·
0 Comments
·
Mark,

Sorry for not enough information.

Took a look at the error message and found that it was caused by Akeeba needing an additional require_once to include additional libraries. Added the include and it started to work properly.

Should have looked at the other extensions that had been updated.

Sorry for jumping to a post.
Looks like I'm all set.
Dan
·
Saturday, 23 September 2017 03:49
·
0 Likes
·
0 Votes
·
0 Comments
·
No problem Dan
·
Saturday, 23 September 2017 17:49
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post