Structure field type not working properly on user fields

On Kirby 2.2.3 version.

I have created skills field that Structure type.

site/accounts/johndoe.php after user updated:

skills: |
  -
  name: php
  level: "10"
  -
  name: html
  level: "8"

On panel listing perfect but trying to get from frontend like following:

$skills = yaml($user->skills());

Do not working properly. Here dump:

var_dump($skills);

array (size=4)
  0 => string '-' (length=1)
  'name' => string 'html' (length=4)
  'level' => string '8' (length=1)
  1 => string '-' (length=1)
1 Like

Note that: i tried same structure field on homepage.

content/home/home.txt output:

Skills: 

- 
  name: php
  level: "10"
- 
  name: html
  level: "8"

And its working perfect but doesnt working on user fields.

Is that the lastest 2.2.3 Kirby version?

Yes

Toolkit license: 2.2.3
Kirby version: 2.2.3
Panel version: 2.2.3

My structure field:

skills:
	label: Skills
	type: structure
	entry: >
		{{name}} ({{level}})
	fields:
		name:
			label: Skill
			type: select
			options: query
			query: 
				page: skills
				fetch: pages
				template: skill
			required: true
			width: 1/2
		level:
			label: Level
			type: select
			default: 5
			options:
				1: 1
				2: 2
				3: 3
				4: 4
				5: 5
				6: 6
				7: 7
				8: 8
				9: 9
				10: 10
			required: true
			width: 1/2

I can reproduce this and opened an issue on GitHub.

Thanks @lukasbestle for referenced issue

I have a similar error but with Kirby 2.4.0

Indeed, I add a structure field in the /site/blueprints/users/default.yml

networks:
    label: Social network
    type: structure
    entry: >
      <strong style='text-transform: uppercase;'>{{network}}</strong> <br> {{username}} <br>  {{url}}
    fields:
      network:
        label: Social network
        type: select
        required: true
        options:
          twitter: Twitter
          linkedin: LinkedIn
      username:
        label: Username
        type: text
        placeholder: @username
        required: true
      url:
        label: URL
        type: URL
        placeholder: https://
        required: true

And from a front-end form, I get an array “group-a” which structure is when I var_dump(get(‘group-a’)) :

array(2) {
  [0]=>
  array(3) {
    ["select-social-network"]=>
    string(7) "twitter"
    ["username-input-social-network"]=>
    string(5) "@zoro"
    ["url-input-social-network"]=>
    string(23) "https://twiter.com/zoro"
  }
  [1]=>
  array(3) {
    ["select-social-network"]=>
    string(8) "linkedin"
    ["username-input-social-network"]=>
    string(3) "@jh"
    ["url-input-social-network"]=>
    string(23) "https://linkedin.com/jh"
  }
}

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.

I’m not supposing to get an Object instead ?

@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.

I have created an issue on GitHub just in case.

1 Like