Create page programmatically - set date

I try to set a given date when creating a page programmatically but the current date/time is set instead.

Is it because I use default: now in my blueprints? Can I overwrite it when creating a page programmatically?

fields: 
  publication: 
      label: Publication date
      type: date
      time: true
      default: now

I create my page like this:

$publication_date = '1551744000'; // March, 05 th 2019
$content = [
			'title' 		=> 'Your Ad title (3-5 words)',
			'description'	=> 'Your Ad description (300-350 characters)',
			'sponsor'	=> 'sponsor',
			'publication'	=> (int)$publication_date
		];
	
		// Generate an unique slug with Stripe transaction_id
		$slug = $event->data->object->payment_intent;

		// Create the "post" page
		kirby()->page('posts')->createChild([
			'content'  => $content,
			'slug'     => $slug,
			'template' => $template,
		]);

I pass the date as an (int) => timestamp, is it the good format?

Thx for you advices

When you create that page, what is stored in the content file before you open the page in the Panel? I don’t think it makes sense to store a Unix timestamp, given that the Panel stores a date in the format YYYY-mm-dd and probably doesn’t make sense of the timestamp (not tested).

So you think I should rather set my date like this?

$content = [
	'title' 	=> 'Your Ad title (3-5 words)',
	'description'	=> 'Your Ad description (300-350 characters)',
	'sponsor'	=> 'sponsor',
	'publication'	=> date( "Y-m-d" , (int)$publication_date );
	];

I give it a try …

Yep works with this format :slight_smile:

Thank you @texnixe

Not sure if this is a bug or I’m doing something wrong but… if I have this field in my blueprint:

image:
  type: files
  multiple: false
  label: Image
  width: 1/2

And I try to add an image programatically it doesn’t work. The field always stays empty.

$new = page('podcasts/category-a')->createChild([
'slug'     	=> $slug,
'template' 	=> 'episode',
'draft'		=> $draft,
'num'		=> $number,
'content' 	=> [
	'title'  	=> $item->get_title(),
	'date' 		=> $item->get_date('d-m-Y'),
	'text'		=> $item->get_description(),
	'number'	=> (int) $number,
	'image'		=> $image,
]
]);

If I rename the image field in the php code to something else then the info does get saved. Are files supposed to be added somehow differently?

If it works with another field name, then I assume it interferes with the native image method, but would have to check what happens in the source code.

I also want to create a page programmatically and want to add an image from the assets. But I dont know what to write instead of your $image? What is behind this variable? How do I refer to an image from the assets?

What exactly do you want to achieve? In the example, only a reference to an image is stored in a field, but not actually a file moved into the files folder.

If you want to store a link to a file, you can create an asset from a file in the assets folder using the asset() helper: https://getkirby.com/docs/reference/templates/helpers/asset, then get the url from that object.

But if you want to use a files field like in the example, it doesn’t accept a URL but needs a file object.

So under specific circumstances I want to create a child of a page programmatically – that is working well so far. But in this specific case I also want to pre-fill the text (what is working) and image fields.
I want to do that, because the page should be online/visible as soon as the page is created and for that I need an image (because of the layout/design). At the moment the page is created but I first need to add an image via the panel. And I want to pre-fill it, and later you still can change the image via the panel.

<?php $downloadChild = $page->createChild([
	"slug" => "downloads",
	"template" => "downloads",
	"content" => [
		"title" => "Downloads",
		"pageIntro" => "text bla",
		"teaser_description" => "text text text"
		"teaser_image" => ??
	]
]); ?>

The text is easy. But how do I add there an image from the assets folder? I thought I could do it like this or so
"teaser_image" => "assets/img/image_name.jpg"

If you want to store something in a files field, the file must actually exist in the content folder, so you would have to move it there from the assets folder. Also, a files field expects that you store the data yaml-encoded.

The alternative would be to not store anything in that field, and only fall back to the image from the assets folder in your template. I think that’s probably the better option.

Ah ok, I kind of thought about that, but I also dont know how to add the file to that content folder. Do you have an example code or a reference to a tutorial? Sorry I am a total beginner in Kirby.

Its a special case, so I dont want to have that image as a default in the blueprint

As I said, I wouldn’t even move the image into the folder that but handle the case that no image is present in the template.

Ah ok, I think I got it :slight_smile: