I want to use the insert module for a file field but I can't get it to be enabled programmaticly. Is there a setting like 'insert' => true, or something like that? Is there any documentation about the these settings because I can't seem to find any info on the web?

  • Why do you need to enable it programatically?
    – Mołot
    Oct 14 '13 at 9:37
  • 3
    @Mołot Well the point of an installation profile is to preset settings etc. I always use an attachment field with insert. So that is why I want to enable it programatically so I don't have to do this everytime I make a new site. I know how to make the fields using field_create_field, my question is now how do I enable this kind of settings.
    – mparryy
    Oct 14 '13 at 9:50

You can enable insert by updating the field instance settings with these two core functions:

for example:

 * Enable insert field settings
function MODULE_field_settings_enable_insert($entity_type, $field_name, $bundle){
  // Load field instance
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  // Build insert settings array, these image styles are just examples
  $insert_settings = array( 
    'insert' => 1,
    'insert_absolute' => 1,
    'insert_styles' => 
    array (
      'image_236w' => 'image_236w',
      'image_237x147' => 'image_237x147',
      'image_486w' => 'image_486w',
      'colorbox__236w' => 'colorbox__236w',
      'colorbox__237x147' => 'colorbox__237x147',
      'colorbox__486w' => 'colorbox__486w',
      'auto' => 0,
      'link' => 0,
      'icon_link' => 0,
      'image' => 0,
      'image_60x60' => 0,
      'image_62x62' => 0,
      'image_62x82' => 0,
      'image_67x67' => 0,
      'image_69x52' => 0,
      'image_71x44' => 0,
      'image_71x71' => 0,
    'insert_default' => 'image_237x147',
    'insert_class' => 'caption',
  // Add the insert settings to the field instance widget settings
  foreach($insert_settings as $key => $setting){
    $instance['widget']['settings'][$key] = $setting;
  // Save the field instance

// Then you can enable insert on a field instance with
MODULE_field_settings_enable_insert('node', 'field_images', 'article')
  • Thanks for your answer, is there a possibility that these settings are set at the creation of the field? so at $field = array([settings]); field_create_field($field); or can it only be done with an update? I did try this but got the following error: PHP Fatal error: Unsupported operand types. I changed it to fit in my profile but apparently something is still going wrong. Any ideas?
    – mparryy
    Oct 14 '13 at 11:26
  • 1
    Insert stores its settings per field instance, not at the global field level. So you may need to use field_create_instance prior to adding the insert settings or just add them at that time. Oct 14 '13 at 11:30
  • I still can't get it to work. I load the instance (works) and then I try this: $instance['widget']['settings']['insert'] = 1; and then save it again: field_update_instance($instance); That should work right?
    – mparryy
    Oct 15 '13 at 9:00
  • 1
    @mparryy there may have been a problem with the $array += $array2 not overwriting the default values. Please try the adjusted answer that uses foreach to force the setting values instead. Tested and working from here. Oct 15 '13 at 9:47

This is pretty simple to do if you are installing Drupal with an installation profile.

Let's say your installation profile name is custom_profile, just add the following line to custom_profile.info file to install insert module when Drupal installs:

dependencies[] = insert
  • This is to enable the module in general and not to enable the insert button at a field.
    – mparryy
    Oct 14 '13 at 12:50
  • @mparryy Exactly! And that is how the question should have been asked. The title asks how to enable the module via installation profile. And the body of the question just says, you need to use the module, but you couldn't enable it programmatically. Please alter your question according to your specific requirements, so that I can delete this answer. But, in current state, I feel this answers your question just right ;-)
    – AjitS
    Oct 14 '13 at 12:54
  • Excuse me? What does this say then: I want to use the insert module for a file field but I can't get it to be enabled programmaticly. So no your answer is not even close if you read this sentence.
    – mparryy
    Oct 14 '13 at 12:58
  • @mparryy My intention wasn't to be rude or something. Please don't take me wrong; English is not my first language. But, when I look at the sentence, I want to use the insert module for a file field but I can't get it to be enabled programmaticly. I get an impression that you want to use the insert module for a certain functionality but can't get it to be enabled programmatically. By it it thought you were referring to the module; because we enable a module and implement an functionality. For the functionality you're trying to implement I think David has given you some useful pointers.
    – AjitS
    Oct 14 '13 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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