Add an image file to a page through CLI

Hi there!

I’m creating many pages from a CSV file. The CLI is an awesome tool and coupled with this library I’ve managed to import almost all fields easily.

Though, I’m stuck on a file field. I need to add an image for each page. All images are in a folder and there’s one column in the CSV where the image filename is specified.

I’m not sure where to start from. :thinking: Couldn’t find any example in that spirit for K4.

What exactly is the missing piece here? Moving the image to the correct location, or how to store the filename in a files field (needs to be stored as array of filenames, as the files field stores its data in yaml format)

Thanks for your answer @texnixe

I realize what I was looking for was already packaged in File::create and with a nice example. :pray:

It’s almost all good, here’s where I am :

$i=0;
	foreach ($csv as $offset => $ref) {
		if($i==0) {
			$reference = $references->createChild([
				'slug'		=> Str::slug($ref['TITRE'],"-"),
				'draft'		=> true,
				'template'	=> 'reference',
				'content'	=> [
					'title' 	=> $ref['TITRE'],
					'refWho'	=> $ref['RÉALISATION'],
					'refWhen'	=> setRefWhen($ref['ANNÉE']),
					'refFormat'	=> setFormat($ref['FORMAT']),
					'refLink'	=> setLink([$ref['LIEN IMDB'],$ref['LIEN UNIFRANCE'],$ref['LIEN ALLOCINE'],$ref['LIEN CREW UNITED']]),
					'customer'	=> setCustomers($ref['PRODUCTION']),
					'travaux'	=> setMissions($ref,$travaux),
					'num'		=> 0,
				],
			]);

			$artwork = File::create([
				'source'	=>	'../filmographie/affiches/'.$ref['VISUEL'].'.jpg',
				'parent'	=>	$reference,
				'filename'	=>	'new-image.jpg',
				'template'	=>	'artwork',
			]);

			$reference = $reference->update([
				'artwork'	=> [$artwork->uuid()],
			]);

			try {
				$reference = $reference->changeStatus('listed');
			} catch (Exception $e) {
				print($e);
			}

		}
		$i++;
	}

The only thing is I get an error when trying to list the page. Though, it’s all looking good in the panel, I can see the draft with all the right information and the page showing well.

Title: Homevideo

----

Travaux:

- page://U3BDTupwJXmLrzpO
- page://g0VYGryqflHRXBg7

----

Customer: teamWorx-Produktion, NDR, BR, ARTE

----

Artwork: - file://TCY43sGTjn0cDzCO

----

Refformat: long-metrage

----

Refwhen: 2010-01-01

----

Refwho: Kilian Riedhof

----

Reflink: https://www.imdb.com/title/tt1969057/?ref_=nv_sr_srsg_0_tt_7_nm_1_in_0_q_Homevideo

----

Metatitle: 

----

Metatemplate: 

----

Usetitletemplate: true

----

Metadescription: 

----

Ogtemplate: 

----

Useogtemplate: true

----

Ogdescription: 

----

Ogimage: 

----

Twittercardtype: 

----

Twitterauthor: 

----

Metainherit: 

----

Num: 0

----

Uuid: cxT1JYwtKY9oLbqS

I get this error :

Kirby\Exception\PermissionException: La page comporte des erreurs et ne peut pas être publiée in dir/kirby/src/Cms/PageRules.php:417
Stack trace:
#0 dir/kirby/src/Cms/PageRules.php(154): Kirby\Cms\PageRules::publish(Object(ReferencePage))
#1 dir/kirby/src/Cms/PageRules.php(97): Kirby\Cms\PageRules::changeStatusToListed(Object(ReferencePage), 15)
#2 dir/kirby/src/Cms/PageActions.php(410): Kirby\Cms\PageRules::changeStatus(Object(ReferencePage), 'listed', 15)
#3 dir/kirby/src/Cms/PageActions.php(285): Kirby\Cms\Page->commit('changeStatus', Array, Object(Closure))
#4 dir/kirby/src/Cms/PageActions.php(254): Kirby\Cms\Page->changeStatusToListed(NULL)
#5 dir/site/commands/importrefs.php(147): Kirby\Cms\Page->changeStatus('listed')
#6 dirglobal/.composer/vendor/getkirby/cli/src/CLI/CLI.php(583): Kirby\CLI\CLI::{closure}(Object(Kirby\CLI\CLI))
#7 dirglobal/.composer/vendor/getkirby/cli/src/CLI/CLI.php(103): Kirby\CLI\CLI->run('importrefs')
#8 dirglobal/.composer/vendor/getkirby/cli/bin/kirby(15): Kirby\CLI\CLI::command('importrefs')
#9 dirglobal/.composer/vendor/bin/kirby(119): include('/Users/pcfhstud...')

I still can rename it in the finder and publish them all so it’s alright.

Alright, I found the issue. I had a ‘max’ on a certain field and my import was reaching over that max so Kirby was avoiding listing. :sweat_smile: I understood when I saw the error in the panel.