To populate my user structure field, I use this function:
function add_to_structure_user( $the_user, $field, $data = array() ) {
$fieldData = $the_user->$field()->yaml();
$fieldData[] = $data;
$fieldData = yaml::encode( $fieldData );
$the_user->update(
array(
$field => $fieldData
)
);
// set the structure field "Social networks"
add_to_structure_user( $site->user(), 'networks', get('group-a') );
But I’ve got an error:
on line: $fieldData = $the_user->$field()->yaml();
Error thrown with message "Call to a member function yaml() on array"
Stacktrace:
#9 Error in xxx/kirb/public/site/controllers/profile.php:165
#8 add_to_structure_user in xxx/kirb/public/site/controllers/profile.php:144
#7 Kirby\Registry\Controller:{closure} in xxx/kirb/public/kirby/core/page.php:1510
#6 PageAbstract:controller in xxx/kirb/public/kirby/kirby/component/template.php:35
#5 Kirby\Component\Template:data in xxxt/apps/kirb/public/kirby/kirby/component/template.php:87
#4 Kirby\Component\Template:render in xxx/kirb/public/kirby/kirby.php:636
#3 Kirby:template in xxx/kirb/public/kirby/kirby.php:624
#2 Kirby:render in xxx/kirb/public/kirby/kirby/component/response.php:29
#1 Kirby\Component\Response:make in xxx/kirb/public/kirby/kirby.php:703
#0 Kirby:launch in xxx/kirb/public/index.php:16
When I var_dump($site->user()->networks()), I get an array.
@gillesvauvarin I’m not sure if this is a feature or or a bug, but definitely the behavior is different than for a normal structure field.
Try to remove the yaml() method, because $the_user->$field already returns an array, not a field object. Similarly, $user->username() returns a string instead of a field object.