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:
<?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:
You have to define the release_artists structure field in the blueprint’s fields section as well.
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
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