How do I access the data from a structure field in my page model's create method?

Hi,

I am using a page model to create a new page with a slug based on user input during the add dialog (Create page slug from two fields) using the awesome Custom Add Field by @steirico.

This works fine so far, but I cannot figure out how to access the data from a structure field in my page model.

My blueprint release.yml (excerpt)

title: Release

    addFields:
    title:
        type: text
        label: Release Title
    release_artists:
        label: Artist
        type: structure
        fields:
            release_artist:
                label: Artist
                type: text
        required: true

and release.php in site/models/ basically is

<?php
use Kirby\Cms\Page;
use Kirby\Toolkit\Str;

class ReleasePage extends Page
{

    public static function create(array $props): Page
    {
        $props['slug'] = Str::slug($props['content']['title']);
        
        return parent::create($props);
    }
}

I tried various methods to my best knowledge but I can’t get the release_artist field to work as part of my slug. I always get the Array to String Conversion error message.

Can anyone help me with this part:
$props['slug'] = Str::slug($props['content'] something something .'-'.['title']);
so that the final slug resembles something like this:

website.com/music/artist1-artist2-artist3-releaseTitle

Thanks a lot!

With which Kirby version are you using the plugin? I’m getting an error a structure field inside the create dialog.

Hi,

this piece of code should solve your problem:

<?php
use Kirby\Cms\Page;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\A;

class ReleasePage extends Page
{

    public static function create(array $props): Page
    {
        $artists = [];
        foreach($props['content']['release_artists'] as $entry) {
            array_push($artists, $entry['release_artist']);
        }

        $props['slug'] = Str::slug(A::join($artists, '-') . '-' . $props['content']['title']);

        return parent::create($props);
    }
}

Two things to keep in mind:

  1. You have to define the release_artists structure field in the blueprint’s fields section as well.
  2. Structure fields in the page dialog seem not to work if one tries to a new page to the site. Adding a subpage to a page worked in my test case. This might be the problem seen by @pixelijn

That’s what I found as well, A::get throws an error, because no array passed.

But I also got an error when adding it to subpages, might have set it up wrongly.

That’s the release.yml which worked for me:

title: Release
fields:
  title:
    type: text
    label: Release Title
  release_artists:
    label: Artist
    type: structure
    fields:
      release_artist:
        label: Artist
        type: text
    required: true

    
addFields:
  title:
    type: text
    label: Release Title
  release_artists:
    label: Artist
    type: structure
    fields:
      release_artist:
        label: Artist
        type: text
    required: true

A fix for using structure fields is under way.

1 Like

Thanks everyone! It works like a charm for me. Thanks a lot!