I've got a plugin that creates PDFs using Michael Rog’s Printmaker plugin. I'm able to successfully save these to a local folder and, using insertFileByLocalPath(), add them to an asset source. Then, by finding the id of the newly created asset, I can save the PDF to an entry.

My problem is that the asset field in the entry is setup to use dynamically-created subfolders, which I want to use to keep the assets for each entry separate from each other.

However, because the dynamically-created subfolder doesn’t exist until an asset is added to the entry, I am adding the PDF to the parent folder using insertFileByLocalPath().

I thought that when I added the asset to the entry and saved it, Craft would dynamically create the subfolder and move the asset into it, but it doesn’t do this. Instead, it just leaves the assets in the parent folder.

Here’s a simplified version of the code I’m using at the moment:

// Create PDF with $vars and $settings
$pdf = craft()->printmaker_pdf->pdfFromTemplate($template, $vars, $settings);

// Save PDF and get URL
$pdfUrl = $pdf->url();

// Get assets parent folder
$folder = craft()->assets->findFolder(array(
    'name' => 'parent-folder'


$response = craft()->assets->insertFileByLocalPath(

// Get id of pdf (newly created Asset)
$pdfId = craft()->assets->findFile(array(
    'filename' => $settings['filename'].'.pdf'

// Add PDF to entry
    'myAssetField' => array($pdfId)

if (craft()->entries->saveEntry($entry)) {
    // Yay! PDF successfully saved to myAssetField

And here’s what the asset folder layout looks like:


Whereas what I want to achieve is this:


Where entry-slug is a dynamically created subfolder for each entry.

Any ideas how I can achieve this, or if it is even possible?

1 Answer 1


I've now worked out how to do this.

As I have a copy of the entry that I want to save the file to, I can work out what the name of the dynamic folder will be called.

// Generate target folder name
$targetFolderName = $entry->slug; // or whatever you want

Next, I see if the folder has already been created:

// See if target folder exists
// I'm searching on the name of the folder, plus the id of the parent folder
// (see question above for how to find this)
$targetFolder = craft()->assets->findFolder(array(
    'name' => $targetFolderName,
    'parent' => $parentFolder->id

if (!$targetFolder) {
    // target folder doesn't exist so we must create it
    $targetFolder = craft()->assets->createFolder($parentFolder->id, $targetFolderName);

    if ($targetFolder->status == 'success') {
        $targetFolderId = $targetFolder->getDataItem('folderId');
    } else {
        throw new Exception(Craft::t("Failed to create new folder."));

} else {
    $targetFolderId = $targetFolder->id;

// Now I can move the PDF from its temporary location into out new assets folder
$response = craft()->assets->insertFileByLocalPath(

From here I followed the same code as in the question to add the asset to the asset field in $entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.