File::Create without source

Hi! I’m trying to create an file using File::Create but is required to supply a “source”. So how can I create a file without having a source? I tried using f::write and it works, but I’m not hable to setup template, and other metadata.

Any help? Thanks!

Whats the use case? File meta belongs to a file - i’m not sure why would want the meta file without a file to go with it.

You can probably do it with File:factory()

$yourfile = File::factory([
  'source'     => 'asourcefile.file',
  'parent'     => $file->parent(),
  'filename'   => 'whateveryouwanttocallit.file',
  'template'   => 'fileblueprintname'
]);

$yourfile->save();

I want to create a css file that is filled with text made by the same code and asign a template to the created file.

I see…

If you have already written the css file to disk with f::write, you can pass that same file through the code above. That should work, and generate the meta for you.

The source needs to be an existing file. This code seems to work, but seems a bit cumbersome:

<?php 
$styles = <<<EOD
body {
  background: blue;
}
EOD;

$file = null;
if (F::write($page->root(). '/test.css', $styles)) {
  $file = new File([
    'source' => $page->file('test.css'),
    'parent' => $page,
    'filename' => 'test.css'
  ]);

}

dump($file);
if ($file) {
  $file->update(['template' => 'css-file']);
}

Without the additional step, you have to refresh the page, it doesn’t get the file at first load with $page->file('test.css')

1 Like

Thank you both! I think now it’s working for me. Best