Access user files

Hello,

I use a model to get the fields data from a user:

<?php

class MembersPage extends Page
{
  public function children()
  {
    $usersPages = [];
    $users      = kirby()->users();
    foreach ($users as $key => $user) {
      $userPages[] = [
        'slug'     => Str::slug($user->id()),
        'num'      => $user->indexOf($users),
        'template' => 'member',
        'model'    => 'member',
        'content'  => [
          'title'    => $user->username(),
          'type'    => $user->member_type()->value(),
          'street'    => $user->street(),
          'city'    => $user->city(),
          'zip'    => $user->zip(),
          'website'    => $user->website(),
          'about'    => $user->about(),
          'userid'    => $user->id(),
          'gallery'    => $user->cover(),
          'canton'    => $user->canton(),
          'profile_image'  => $user->images()
        ]
      ];
    }
    return Pages::factory($userPages, $this);

  }
}

I do not understand how I get the images from gallery(), how I get all files from the user but I do not know those from the one field.

			<?php
				$image = $kirby->user($page->userid())->gallery()->toFile(); 
				$thumbURL = $image? $image->resize(1000)->url(): '';
				echo $thumbURL;
				/*
				$imgs = $kirby->user($page->userid())->images();

				foreach($imgs as $image):
					if($image->name() != "profile"):

					echo $image->thumb([
						'width'   => 1000,
						'quality' => 100
					]);
					endif;
				endforeach;
				*/
			?>

What do you get if you do

dump($page->gallery());
Kirby\Cms\Field Object
(
    [gallery] => Kirby\Cms\Field Object
        (
            [cover] => - hin_ist_hin-plakat-web.jpg
- hin_ist_hin_web_055.jpg
- matto-plakat.jpg
- matto_andrinwinteler_1120_web.jpg
- mit_der_zeit_muss_man_gehen_0532-web.jpg
- mit_der_zeit_muss_man_gehen_0112-web.jpg
- dot_presse_hoch_1.jpg
- dot_presse_hoch_3.jpg
- dot_presse_quer_2.jpg
        )

)

So getting a single image from that field works? But you want to get all images from that field?

yes all images from this field, I tried this but I get only the first image.

<?php


				$str = $page->gallery();
				$image_gallery = explode("- ",$str); 
				$imgs = $kirby->user($page->userid())->images();

				foreach($imgs as $image):
					echo $image->filename();
					if (in_array($image->filename(), $image_gallery)):
						

					echo $image->thumb([
						'width'   => 1000,
						'quality' => 100
					]);
					endif;
				endforeach;
				
			?>

Why I get only the last image of the list?

What is the result of dump($image_gallery)?

Array
(
    [0] => 
    [1] => bienen-brot-und-blumen.jpeg

    [2] => die-geschichte-vom-wunder-apfel.jpeg

    [3] => kittels-traum-kathrin-irion.jpg
)

I just realized that you have this nested field object which comes from storing the fields into your content array instead of the field values. For the type you store the values, but for the rest your store the fields

that mean the way how I create an array from the strings is wrong ?

$image_gallery = explode("- ",$str);

No, that’s not what I wrote, but I would use

$image_gallery = $page->gallery()->yaml();

to prevent the empty array item.

1 Like

Thank you @texnixe.

since kirby 3.4.0 I receive the following error message:

  'toStructure' => function (Field $field) {
        try {
            return new Structure(Data::decode($field->value, 'yaml'), $field->parent());
        } catch (Exception $e) {
            if ($field->parent() === null) {
                $message = 'Invalid structure data for "' . $field->key() . '" field';
            } else {
                $message = 'Invalid structure data for "' . $field->key() . '" field on parent "' . $field->parent()->title() . '"';
            }
 
            throw new InvalidArgumentException($message);
        }
    },

That doesn’t look like an error message but like a piece of code :wink:. Maybe this code is throwing the error message, but it would be more useful to know which part of your code throws which of these 2 possible error message…

error message:

Invalid structure data for "blackboard" field on parent "Theater Roos und Humbel"

for example this part:

				<ul class="shows">
					<?php  
					foreach ($page->dates()->toStructure() as $dates): ?>
						<ul class="show">
						<?php if($dates->website()!= ""): ?>
							<a href="<?php echo $dates->website(); ?>">
						<?php endif ?>
						<li class="event-date"> <?php echo date("d.m.Y, H:i", strtotime($dates->date())); ?></li>
						<li class="event-title"> <?php echo $dates->title(); ?></li>
						<li> <?php echo $dates->location().", ".$dates->city(); ?></li>
						<?php if($dates->website()!= ""): ?>
							</a>
						<?php endif ?>
						</ul>
					<?php endforeach; ?>	

				</ul>
			</section>

			<section class="grid-col-40 gallery">

				<?php
					$image_gallery = $page->cover()->yaml();

					foreach($image_gallery as $image):
						$imgs = $kirby->user($page->userid())->image($image);
						if ($image):	
							echo $imgs->thumb(
								[
									'autoOrient' => true,
									'crop'       => false,
									'blur'       => false,
									'grayscale'  => false,
									'width'     => 500,
									'quality'    => 90
								]
							);
						endif;
					endforeach;
		
				?>

			</section>

what does mean: Invalid structure data ?

I don’t know. What does you blueprint look like? Is there a blackboard field? Do you use any plugins?

the data comes from the user profiles

 title: Member
    permissions:
      access:
        panel: true
        site: true
        settings: false
        users: false
      files:
        create: true
        changeName: true
        delete: true
        replace: true
        update: true
      site:
        update: false
      pages:
        create: false
        changeTemplate: false
        changeTitle: false
        changeSlug: false
        delete: false
        hide: false
        sort: false
        update: false
      user:
        create: false
        changeName: true
        changeEmail: true
        changeLanguage: true
        changePassword: true
        changeRole: false
        delete: false
        update: true
        hide: true


      users:
        *: false
        


    columns:
      - width: 1/2
        fields:
          member_type:
            label: type
            type: select
            translate: false
            options:
              - KünstlerIn
              - VeranstalterIn
              - Museen
              - Ausbildung  
              - Figurenspieltherapie
              

          street:
            label: 
              de: Strasse
              fr: Rue
              it: Strada
            type: text
          zip:
            label: 
              de: PLZ
              fr: CP
              it: CP
            type: text
            width: 1/4
          city:
            label:
              de: Ort
              fr: Lieu
              it: Lieu
            type: text
            width: 3/4
         canton:
            label: 
              de: Kanton
              fr: Canton
              it: Cantone
            type: select
            required: true
            translate: false
            options:
              AG: AG
              AI: AI
              AR: AR
              BE: BE
              BL: BL
              BS: BS
              FR: FR
              GE: GE
              GL: GL
              GR: GR
              JU: JU
              LU: LU
              NE: NE
              NW: NW
              OW: OW
              SG: SG
              SH: SH
              SO: SO
              SZ: SZ
              TG: TG
              TI: TI
              UR: UR
              VD: VD
              VS: VS
              ZG: ZG
              ZH: ZH


      - width: 1/2
        fields:
          website:
            label: Website
            type: url
          twitter:
            label: Twitter
            type: text
            icon: twitter
            placeholder: @username
          instagram:
            label: Instagram
            type: text
            icon: instagram
            placeholder: username
          facebook:
            label: Facebook
            type: text
            icon: facebook
            placeholder: username

      - width: 1/1
        fields:
          about:
            label:  
              de: Über
              fr: über
              it: über
            type: markdown
            width: 2/3
          cover:
            label: 
              de: Datei auswählen
              fr: Select files
              it: Select files
            type: files
            layout: cards
            max: 3
            width: 1/3
            translate: false
            size: small

      - width: 1/1
        fields:
          info:
            label: Info
            type: info
            text: |
             Du hast hier die Möglichkeit Inhalte fuer den Spielplan Schwarze Brett zu erstellen.
             Bitte erstelle fuer jede Sprache die Eintraege einzeln.
          dates:
            label:
              de: Spielplan
              fr: Spielplan
              it: Spielplan
            type: structure
            fields:
              title:
                label: 
                  de: Titel
                  fr: Titel
                  it: Titel
                type: text
                required: true
              date:
                label: Date
                type: date
                default: today
                time: true
              location:
                label:
                  de: Veranstaltungsort
                  fr: Veranstaltungsort
                  it: Veranstaltungsort
                type: text
                width: 1/3
              city:
                label:
                  de: Ortschaft
                  fr: Ortschaft
                  it: Ortschaft
                type: text
                width: 1/3
              website:
                label: 
                  de: Website
                  fr: Website
                  it: Website
                type: url
                width: 1/3
              festival:
                label: 
                  de: Festival
                  fr: Festival
                  it: Festival
                type: checkboxes
                width: 1/3
                options:
                  - Festival

          
          blackboard:
            label:
              de: Schwarzes Brett
              fr: Petites Annonces
              it: Piccoli annunci
            type: structure
            fields:
              title:
                label: 
                  de: Titel
                  fr: Titel
                  it: Titel
                type: text
                required: true
              text:
                label: Text 
                type: markdown
              category:
                label: 
                  de: Kategorie
                  fr: Kategorie
                  it: Kategorie
                type: checkboxes
                required: true
                options:
                  - Material
                  - Kurse
                  - Vernetzung
              cover:
                label: Select files...
                type: files
                layout: cards
                max: 1
                width: 1/1
                size: small
              end_date:
                label: 
                  de: Verfalldatum
                  fr: Verfalldatum
                  it: Verfalldatum
                type: date
                required: true
              created:
                label: created
                type: date
                time: true
                default: now
                disabled: true

And where is the connection between the code snippet you posted above and this user profile? I’m completely lost.

But this error didn’t come up before Kirby 3.4? Have you updated to the final release version? Could be a bug after all, but I can’t tell from here. Do you have proper debugging in place (xdebug?).

every user is like a page as child pages of these pages serve the structurfields.
please have look on my first post.

But this error didn’t come up before Kirby 3.4?

no