# Problem creating a page with prop $page-\>cover()

**URL:** https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977
**Category:** Questions
**Tags:** v3
**Created:** [November 6, 2020, 10:53pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977 "2020-11-06T22:53:34Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 6, 2020, 10:53pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/1 "2020-11-06T22:53:34Z")

</div>

When trying to create a page and passing $page-\>cover() (Being a files field) to one of its content props, this prop is empty ?!

Tipps appreciated …

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 6, 2020, 11:36pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/2 "2020-11-06T23:36:05Z")

</div>

I’m missing some context here. Where is this page created, via the Panel, programmatically? And where and when do you call `$page->cover()` then?

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 6:44am UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/3 "2020-11-07T06:44:16Z")

</div>

I got a migration script, loading in Kirby and then iterating over some pages, creating a set of pages using some of the page information, some other information from APIs etc.

Inside the loop, let’s say the original page is `$page` and the one to be created is created by `$otherPage->createChild()`:

```php
$otherPage->createChild([
  'slug' => $page->slug(),
  'template' => 'some-template',
  'content' => [
    'title' => $page->title(),
    'cover' => $page->cover(),
    // ...
  )],
]);

```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 8:48am UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/4 "2020-11-07T08:48:17Z")

</div>

You have to assign the value of the field, not the field object:

```auto
  'content' => [
    'title' => $page->title()->value(),
    'cover' => $page->cover()->value(),
    // ...

```

`$page->cover()` etc. return an object of the `Field` class, but we need a string here.

Please note that if you want to work with this page object after creation, you have to store it in a variable.

```auto
$newPage = $otherPage->createChild([
  'slug' => $page->slug(),
  'template' => 'some-template',
  'content' => [
    'title' => $page->title()->value(),
    'cover' => $page->cover()->value(),
    // ...
  )],
]);

echo $newPage->cover();

```

In an echo context like the last line, a string is returned, because the magic method `__toString()` returns that string from the object.

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 8:51am UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/5 "2020-11-07T08:51:33Z")

</div>

Already tried that, too - doesn’t work (at least for me). Also tried with `toFile()->filename()` ☹

Will try again, though. Something smells fishy

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 8:52am UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/6 "2020-11-07T08:52:54Z")

</div>

To store the content of a files field, you have to store it in yaml format.

```auto
Data::encode($page->cover()->yaml(), 'yaml');

```

Should work.

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 8:53am UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/7 "2020-11-07T08:53:18Z")

</div>

I get it, good pointer!

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 10:50am UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/8 "2020-11-07T10:50:30Z")

</div>

it doesn’t work! 😮

I don’t get why your approach doesn’t work … `dump()`ing it gives the correct cover image …

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 2:10pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/9 "2020-11-07T14:10:17Z")

</div>

> [@texnixe](#):
>
> `Data::encode($page->cover()->yaml(), 'yaml')`

This is correct, just tested it.

However, unless your code above only has a copy/paste issue, the syntax is not correct, the content array is not closed and the number of brackets is not correct either.

If that’s not the issue: Do you use the cover method from the Starterkit album model or something similar? If that is active, the code doesn’t work (but would then throw an error).

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 2:27pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/10 "2020-11-07T14:27:32Z")

</div>

I typed in the example code, so the missing bracket is only there, not in the actual code - but how great of you distinguishing both cases!

`cover` is the name of the `files` field, nothing special there … this is the content file:

```nohighlight
----

Cover:

- eine-schwester_bastien-vives.jpg

----

```

… the actual code:

```php
$page = page('lesetipps')->children()->listed()->flip()->first();
$kirby->impersonate('kirby');

try {
    $child = page('buecher')->createChild([
        'slug' => $page->slug(),
        'template' => 'book',
        'content' => [
            'cover' => Data::encode($page->cover()->yaml(), 'yaml'),
            'title' => $page->title(),
            'isbn' => $page->isbn(),
            'shop' => $page->shop(),
            'book_title' => $page->book_title(),
            'book_subtitle' => $page->book_subtitle(),
            'author' => $page->author(),
            'illustrator' => $page->illustrator(),
            'narrator' => $page->narrator(),
            'translator' => $page->translator(),
            'participants' => $page->participants(),
            'page_count' => $page->page_count(),
            'publisher' => $page->publisher(),
            'age' => $page->age(),
            'price' => $page->price(),
            'binding' => $page->binding(),
            'categories' => $page->categories(),
            'topics' => $page->topics(),
            'hasAward' => $page->hasAward(),
            'award' => $page->award(),
            'awardEdition' => $page->awardEdition(),
            'leselink' => $page->leselink(),
            'isAudiobook' => $page->isAudiobook(),
        ],
    ]);

    // foreach ($page->files() as $file) {
    // $file->copy($child);
    // }

} catch (\Throwable $th) {
    echo $th;
}

```

… and the blueprint portion is this:

```auto
fields:
  cover:
    label: Cover
    type: files
    query: page.images.filterBy('extension', 'in', ['jpg', 'jpeg', 'png'])
    layout: cards
    multiple: false
    size: huge
    image:
      ratio: 2/1
      cover: true
    info: "{{ file.dimensions }} ({{ file.niceSize }})"

```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 3:09pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/11 "2020-11-07T15:09:41Z")

</div>

And what is the result in the newly created content file? What does it store as `cover`? Nothing at all? While all other fields are stored correctly?

And what do you get when you do this:

```auto
$page = page('lesetipps')->children()->listed()->flip()->first();
dump($page->cover());

```

The result should be a field object.

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 3:10pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/12 "2020-11-07T15:10:49Z")

</div>

That is absolutely correct …

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 3:11pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/13 "2020-11-07T15:11:46Z")

</div>

See my edited post above.

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 3:16pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/14 "2020-11-07T15:16:13Z")

</div>

```nohighlight
Kirby\Cms\Field Object
(
    [cover] => - >
  eine-schwester_bastien-vives.jpg
)

```

… and for `$child` it gives this:

```nohighlight
Kirby\Cms\Field Object
(
    [cover] => 
)

```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 3:32pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/15 "2020-11-07T15:32:01Z")

</div>

That is all very weird.

If you do this in a fresh Starterkit, do you run into the same issue?

E.g in a Starterkit’s home template, I put this code for testing:

```auto
$p = page('photography')->children()->first();
$kirby->impersonate('kirby');
$newPage = $page->createChild([
  'slug' => $p->slug() . '-' . microtime(),
  'template' => 'some-template',
  'content' => [
    'title' => $p->title()->value(),
    'cover' => Data::encode($p->cover()->yaml(), 'yaml')
  ],
]);

```

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 3:45pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/16 "2020-11-07T15:45:49Z")

</div>

It gives this:

```nohighlight
Argument 1 passed to Kirby\Data\Data::encode() must be of the type array or null, object given, called in /home/two-brain/Downloads/starterkit-master/site/templates/home.php on line 20

```

PHP version: PHP 7.4.11 - `php -S localhost:3000 kirby/router.php`

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 3:49pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/17 "2020-11-07T15:49:26Z")

</div>

Ah, sorry, in the Starterkit you first have to remove the cover method from the album model because it gets in the way.

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 5:03pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/18 "2020-11-07T17:03:59Z")

</div>

Works as expected:

```nohighlight
Title: Trees

----

Cover:

- monster-trees-in-the-fog.jpg

----

Text: 

```

This is really weird … I also removed all plugins, but nothing works.

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 7, 2020, 5:21pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/19 "2020-11-07T17:21:11Z")

</div>

Where are you executing your code?

---

<div class="post-metadata">

### Author: ![S1SYPHOS](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/s1syphos/32/3671_2.png) [@S1SYPHOS](https://forum.getkirby.com/u/S1SYPHOS)
#### Post date: [November 7, 2020, 6:25pm UTC](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977/20 "2020-11-07T18:25:08Z")

</div>

It’s a separate script, loading Composer’s autoloader, initiating Kirby and then calling above code - but executing it from inside a template resulted in the same

I’m lost in this one, especially since it’s hard to debug.

[Next page](https://forum.getkirby.com/t/problem-creating-a-page-with-prop-page-cover/19977.md?page=2)
