I've created a module that creates a content type. That content type has an image field. I would like to programmatically add a default image to that field for this content type. I'm stuck.

      // Create default hero image for campaigns

      $module_path = backdrop_get_path('module', 'crowdfunding_campaign');
      $image_path = $module_path . '/images/hero-background.jpg';
      $image_object = file_get_contents($image_path);
      file_save_data($image_object, 'public://', 'hero-background.jpg');

      // Field SPPX ID 
      $field = array(
          'field_name' => 'field_hero_image',
          'type' => 'image',
          'cardinality' => '1',
          'settings' => array(
            'uri_scheme' => 'public',
            'default_image' => '',
          ),
        );
      field_create_field($field);
  
      $instance = array(
          'field_name' => 'field_hero_image',
          'entity_type' => 'node',
          'label' => 'Hero Image',
          'bundle' => 'campaign',
          'description' => st('Banner image for top of page.'),
          'widget' => array(
              'type' => 'image_image'
          ),
          'display' => array(
              'default' => array(
                  'label' => 'hidden',
                  'type' => 'image'
              )
          ),
          'default_value' => null,
          'default_value_function' => null,
          'setting' => array(
            'file_directory' => '',
            'file_extensions' => 'png gif jpg jpeg',
            'default_image' => 'public://hero-background.jpg',
          )
      );
      field_create_instance($instance);

Accepted answer

Hey @Klonos! Thanks. 

I had googled the issue and had seen the second article mentioned there, but not the first. I wan't able to make that information help me. 

HOWEVER, by just thrashing around long enough and testing enough possibilities out, I have a working solution for now. 

      // Create default hero image for campaigns

      $module_path = backdrop_get_path('module', 'crowdfunding_campaign');
      $image_path = $module_path . '/images/hero-background.jpg';
      $image_object = file_get_contents($image_path);
      file_unmanaged_save_data($image_object, 'public://hero-background.jpg');

      // Field SPPX ID 
      $field = array(
          'field_name' => 'field_hero_image',
          'type' => 'image',
          'cardinality' => '1',
          'settings' => array(
            'uri_scheme' => 'public',
            'default_image' => 'public://hero-background.jpg',
          ),
        );
      field_create_field($field);

I believe that I was stuck for two reasons. One, I was trying to add the default image to the field_instance rather than the field itself. Two, I was trying to make it work with file_save_data (which may be the better solution, but is also more complicated). By switching to file_unmanaged_save_data it's a little more straight forward and seems to be working for me. 

The file_save_data was adding the image to the Backdrop file management system (which sounds good). But, that also meant changing it's title and making it more difficult for me to understand how the path was being generated. file_unmanaged_save_data is just a simpler function that does less, but seems to work.

If anyone has ideas on how to do this better, let me know. But, for now I've got a working solution.

Comments

Hey @Klonos! Thanks. 

I had googled the issue and had seen the second article mentioned there, but not the first. I wan't able to make that information help me. 

HOWEVER, by just thrashing around long enough and testing enough possibilities out, I have a working solution for now. 

      // Create default hero image for campaigns

      $module_path = backdrop_get_path('module', 'crowdfunding_campaign');
      $image_path = $module_path . '/images/hero-background.jpg';
      $image_object = file_get_contents($image_path);
      file_unmanaged_save_data($image_object, 'public://hero-background.jpg');

      // Field SPPX ID 
      $field = array(
          'field_name' => 'field_hero_image',
          'type' => 'image',
          'cardinality' => '1',
          'settings' => array(
            'uri_scheme' => 'public',
            'default_image' => 'public://hero-background.jpg',
          ),
        );
      field_create_field($field);

I believe that I was stuck for two reasons. One, I was trying to add the default image to the field_instance rather than the field itself. Two, I was trying to make it work with file_save_data (which may be the better solution, but is also more complicated). By switching to file_unmanaged_save_data it's a little more straight forward and seems to be working for me. 

The file_save_data was adding the image to the Backdrop file management system (which sounds good). But, that also meant changing it's title and making it more difficult for me to understand how the path was being generated. file_unmanaged_save_data is just a simpler function that does less, but seems to work.

If anyone has ideas on how to do this better, let me know. But, for now I've got a working solution.