2

I've just installed the Insert module. I have many many content types in my site and all of them contains the same "Body" field. It would take me much time to attach an Insert image-field to each one of them manually and I need a way to do that at once.

How will you achieve this goal? If you drop a module's name please also detail in steps where and what will you do to achieve it.

4
+100

You may add this code snippet in the install file in your custom module and run update.php. It works fine, I've tested this code on my local site

<?php
/**
 * Add an image field to a node.
 */
function my_module_update_7001() {
  $field_name = 'field_insert_image';

  // Make sure the field doesn't already exist.
  if (!field_info_field($field_name)) {
    // Create the field.
    $field = array(
      'field_name' => $field_name,
      'type' => 'image',
    );
    field_create_field($field);
  }

  $content_types = node_type_get_types();
  $exclude = array('example');
  foreach ($content_types as $content_type) {
  if (!in_array($content_type->type, $exclude)) {
    $instance = field_info_instance('node', $field_name, $content_type->type);
    if (!$instance) {
      // Create the instance.
      $instance = array(
        'field_name' => $field_name,
        'entity_type' => 'node',
        'bundle' => $content_type->type,
        'label' => 'Image Insert',
        'description' => 'An image for the article.',
        'required' => TRUE,
        'settings' => array(
          'file_extensions' => 'png jpg jpeg',
          'file_directory' => '',
          'max_filesize' => '20 MB',
          'alt_field' => 1,
          'title_field' => 1,
          'max_resolution' => '300x200',
          'min_resolution' => '200x125',
          'default_image' => 0,
         ),
         'widget' => array(
          'type' => 'image_image',
          'module' => 'image',
          'active' => 1,
          'settings' => array(
            'insert' => 1,
            'insert_absolute' => 1,
            'insert_styles' => array_keys(insert_styles_list()),
          ),
         ),
         'display' => array(
          'default' => array(
           'label' => 'hidden',
           'type' => 'image',
           'weight' => 0,
           'settings' => array(),
           'module' => 'image',
         ),
         'full' => array(
           'label' => 'hidden',
           'type' => 'image',
           'weight' => 0,
           'settings' => array(),
           'module' => 'image',
         ),
         'teaser' => array(
           'label' => 'hidden',
           'type' => 'image',
           'weight' => 0,
           'settings' => array(),
           'module' => 'image',
         ),
       )
      );

      field_create_instance($instance);
    }
  }
 }
}
  • I guess it doesn't matter If I already attached "Insert" to two "Body" fields manually right? I would anyhow attach it to all other existing fields? (I've yet to learn PHP and thus wonder). – JohnDoea Oct 13 '15 at 21:51
  • Yes, I add new field only if it doesn't exist in the content type already. You just need to change $field_name variable with your field name. And also you can exclude some content types by adding their names in the $exclude array. Please do not forget to backup your database before applying these changes. – Andrew Oct 14 '15 at 3:47
0

Your requirements is not very clear. Insert module allows user to insert the image in the body field in a single click using the insert button.

The short answer to your question is NO short method. Even if you're using a shared field and the original field has been configured to use Insert button you still have to configure each field to enable the Insert button. It is possible to do this via Drupal API but you have to be familiar on field apis at least or via hook_update_N. As of my understanding of your requirements is you are looking for a fastest method to enable Insert for each content type. So yeah your only option is to enable it manually.

0

My suggestion for you is to get all content type (also you can exclude some of them that you don't want to add) then in one foreach with field_create_instance add your desire field to content type.

I provide some help code (I don't test it right now and just typing them) you can achieve what you with some think like below code.
(you can also get your field info with field_info_instances)
First register a menu, in callback of menu call a function, in function in foreach of your content types you want add field with field_create_instance.

  <?php 

  function  autoaddinsert_menu(){
    $items =array();

    $items['admin/addautoinsert'] =array(
      'title' => 'Add isert field',
      'description' => 'add insert  field automatically to all content type',
      'page callback' => 'autoinsert_addfield',
      'access callback' > true,
    );
    return $items;
  }


  function autoinsert_addfield(){


    $ctypes=node_type_get_types(); //
    foreach($ctypes as $content_type){

    // Check if our field is not already created.
      $yourfieldname='field_yourinsert';// @todo put your  machine name of insert field here
      if (!field_info_field($yourfieldname)) {

      // Create the field base.
        $field = array(
          'field_name' => $yourfieldname, 
          'type' => 'image',  //@todo put your field type here
        );
        field_create_field($field);

        // Create the field instance on the bundle.
        $instance = array(
          'field_name' => $yourfieldname, 
          'entity_type' => $content_type->type, 
          'label' => 'My Field Name', //@todo put your label here
          'bundle' => $content_type->type, 
          // If you don't set the "required" property then the field wont be required by default.
          'required' => TRUE,

          'widget' => array(
            'type' => 'textfield',
          ), 
        );
        field_create_instance($instance);
      }
    }    
  }
  • The widget type is wrong – Clemens Tolboom Oct 14 '15 at 14:48
  • @ClemensTolboom This is just a clue to what he should do and is not exact solution – Yusef Oct 14 '15 at 16:40

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.