With the Zeever project, I'm having some difficulty adding files/images to blocks on a permanent basis. My current approach is to:

  1. Upload the images using the following code:
    // Prepping images for news block
    $field_image_dir = 'public://news';
    file_prepare_directory($field_image_dir, FILE_CREATE_DIRECTORY);
    $news_image = array(
      'zeever-logo-chatgpt.webp',
      'zeever-open-source-chatgpt.webp',
      'first-post-chatgpt.webp',
      'simplo-dragon-chatgpt.webp',
      'office-chatgpt.webp',
      'philosophy-chatgpt.webp',
      'teamwork-chatgpt.webp',
      'faq-chatgpt.webp',
    );
    $fids = [];

    foreach ($news_image as $image_filename) {
      $image_url = BACKDROP_ROOT . '/' . $module_path . '/' . 'images/'. $image_filename;
      $moved_file = file_unmanaged_copy($image_url, $field_image_dir);

      $file = entity_create('file', array(
        'filename' => $image_filename,
        'uri' => $moved_file,
        'uid' => 1,
      ));

      $file->save();
      $fids[] = $file->fid;
    }

2. The add the config file for the block that includes the fid number for the required image. 

{
    "_config_name": "image_grid_block.block.about_block_on_about_page",
    "parent": "about_block_with_images",
    "label": "About Block on About Page",
    "machine_name": "about_block_on_about_page",
    "title": "About",
    "content_1": "Office",
    "content_2": "Philosophy",
    "content_3": "Team Work",
    "link_1": "About [2]",
    "link_2": "Freelance Photographer Portfolio [18]",
    "link_3": "Tech Startup [15]",
    "highlighted_image": "none",
    "image_1": "19",
    "image_2": "20",
    "image_3": "21",
    "image_style": ""
}

This APPEARS to work fine as the block shows up on the site with the proper image. However, after inspecting the images I can see that all the images I loaded to blocks this way are listed as Temporary, while the images loaded into nodes are permanent. The Temporary images eventually get cleaned away and the site appears broken. 

 

I assume the problem is roughly, that Backdrop knows that I've added the image to the site and the block initially knows where to find the image. But, Backdrop doesn't know that the image is actually being used and eventually deletes it, causing the block to break. 

STEPS TO RECREATE:

  1. Install the Digital Agency Recipe Package (v. 1.x-1.0.0-beta19) into a demo Backdrop CMS site
  2. Review the files at: admin/content/files

QUESTION:

How do I programmatically create a block with an image, so that Backdrop CMS knows the image is being used and does not treat the image as if it's only temporary and eventually clean it away. 

USEFUL INFORMATION;

Most of the work being discussed here is being done in this file.
https://github.com/backdrop-contrib/digital_agency/blob/1.x-1.x/digital_...

Comments

OK, I got some excellent help on this issue during office hours today. Thanks Justin!

Here is how it was explained to me. 

If a file in Backdrop CMS is NOT assigned a usage relationship with an entity, it is considered a temporary file and will periodically be removed from the files directory. 

Files assigned to a node are listed as being used by that node. Files assigned to a block are assigned to that block type. See:

Files that are added directly to BackdropCMS through the File Manager are assigned to themselves. 

The way I was adding files to the site was not giving them a usage relationship for blocks. This could be handled with 

file_usage_add($file, 'file', 'file', $file->fid);

So it would be best if I connected my files directly to the block type they are assigned to, but I could also simply connect them to themselves as done when loading a file through the file manager. For now, I'm using the second solution. 

Will show how that looks in my code in the next comment.

    // Prepping images for blocks
    $field_image_dir = 'public://blocks';
    file_prepare_directory($field_image_dir, FILE_CREATE_DIRECTORY);
    $block_image = array(
      'office-chatgpt.webp',
      'philosophy-chatgpt.webp',
      'teamwork-chatgpt.webp',
      'faq-chatgpt.webp',
    );

    foreach ($block_image as $image_filename) {
      $image_url = BACKDROP_ROOT . '/' . $module_path . '/' . 'images/'. $image_filename;
      $moved_file = file_unmanaged_copy($image_url, $field_image_dir);

      $file = entity_create('file', array(
        'filename' => $image_filename,
        'uri' => $moved_file,
        'uid' => 1,
      ));

      $file->save();
      file_usage_add($file, 'file', 'file', $file->fid);
      $fids[] = $file->fid;
    }