I am a long time user of this module. It's my number on choice for an image module. However, one thing I would like that it doesn't currently offer (as far as I can tell) is the ability to use the imagepicker on other forms that aren't node forms. I create a lot of modules, many of which create their own editable pages, and need image support. I've found other modules for this task, but imagepicker is the ideal solution for me. So it would be nice if it were possible to use this module on other forms.

I'm thinking (fully hypothesis - no testing done), that it could be as simple as adding a textarea to the settings page on which form IDs could be added, one per line, and saved using variable_set(). Then in hook_form_alter(), the variable could be exploded by PHP_EOL (the new lines), and the $form_id tested against the resulting array using in_array(). If the $form_id is in the array, then the imagepicker could be added to the form.

Any possibility of incorporating this? The ability to use the module in custom non-node modules would be great! Thanks.

Comments

hutch’s picture

I like the idea but I'm not sure how to test this out, the additional stuff in imagepicker_admin is straightforward and I guess the line(s) added to it could be turned into an array with explode() on a linefeed and as you say test with in_array() in function imagepicker_form_alter(). wether it would place the insert where a bit of testing would reveal I guess.
If you have anywhere you can try this out have a go, it could be added after elseif(block), eg elseif($other) or somesuch. If I can find some time I'll try it out but I'm juggling quite a few different things at the moment ;-)

jaypan’s picture

I'll see if I can put together a patch in the next few days.

zach harkey’s picture

Would this allow us to use imagepicker on Panels custom content panes?

jaypan’s picture

Yes, if it works, it would allow that.

Hutch - As a proof of concept, I've managed to include the imagepicker iframe on other forms. However, the insert button doesn't work when I do this. I'm not clear enough on the inner workings of the module to be able to fix this.

What I've done so far is the following:
1) Changed imapicker_form_alter() (in imagepicker.module) to the following:

/**
 * Implementation of hook_form_alter().
 */
function imagepicker_form_alter(&$form, &$form_state, $form_id) {
  global $user;

  $node_type = FALSE;
  $comment = FALSE;
  $block = FALSE;
  $custom_form = FALSE;
  $weight = 1;
  if (isset($form['#node'])) {
    $node = $form['#node'];
    $node_types = node_get_types('names');
    $node_type = in_array($node->type, variable_get('imagepicker_node_types_enabled', array_keys($node_types)), TRUE);
  }
  elseif (module_exists('comment') && variable_get('imagepicker_comment_enabled', 0) && preg_match('/comment_form$/i', $form_id) ) {
    $comment = TRUE;
    $weight = 1;
  }
  // blocks
  elseif (( $form_id == 'block_add_block_form' || $form_id == 'block_admin_configure' ) && variable_get('imagepicker_blocks_enabled', 0)) {
    $block = TRUE;
    $weight = 1;
  }
  if(!$node_type && !$comment && !$block) {
    $custom_forms = explode(PHP_EOL, variable_get('image_picker_custom_forms', ''));
    if(is_array($custom_forms) && count($custom_forms)) {
      foreach($custom_forms as $cf) {
        if(trim($cf) == $form_id) {
          $custom_form = TRUE;
          break;
        }
      }
    }
  }
  if (user_access('use imagepicker') && ($node_type || $block || $comment || $custom_form)) {
    $width = variable_get('imagepicker_advanced_iframe_width', "100%");
    if ( isset($user->imagepicker_advanced_iframe_width) && $user->imagepicker_advanced_iframe_width ) {
      $width = $user->imagepicker_advanced_iframe_width;
    }
    if (! preg_match("/%$/", $width)) {
      $width .= 'px';
    }
    $height = variable_get('imagepicker_advanced_iframe_height', 500);
    if (isset($user->imagepicker_advanced_iframe_height) && $user->imagepicker_advanced_iframe_height ) {
      $height = $user->imagepicker_advanced_iframe_height;
    }
    $border = variable_get('imagepicker_advanced_iframe_border', "0");
    if (isset($user->imagepicker_advanced_iframe_border)) {
      $border = $user->imagepicker_advanced_iframe_border;
    }

    if (imagepicker_get_thickbox_perms()) {
      $iurl = l('Run imagepicker', 'imagepicker' );
    }
    elseif (imagepicker_get_colorbox_perms()) {
      $iurl = l('Run imagepicker', 'imagepicker', array(
        'query' => array('width' => $width, 'height' => $height, 'iframe' => 'true'),
        'attributes' => array('class' => 'colorbox-load')
      ));
    }
    else {
      $iurl = '<iframe id="imagepicker" style="width: '. $width .'; height: '. $height .'px; border: '. $border .';" src="'. url('imagepicker') .'">Imagepicker requires iframe support.</iframe>';
    }

    if (($node_type && preg_match('/node_form$/i', $form_id) ) || $comment || $custom_form) {
      $form['body_field']['body_filter']['file_upload'] = array(
        '#type' => 'fieldset',
        '#title' => t('Image picker'),
        '#collapsible' => 1,
        '#collapsed' => variable_get('imagepicker_advanced_collapsed', 0),
        '#weight' => $weight,
      );

      $form['body_field']['body_filter']['#weight'] = 2;
      $form['body_field']['body_filter']['file_upload']['mpframe'] = array(
        '#type' => 'markup',
        '#value' => '<div id="imgp_tb">'. $iurl .'</div>',
      );
      $form['body_field']['#prefix'] = '<a name="body_hash"></a>'. $form['body_field']['#prefix'];
    }
    elseif ($block) {
      $form['block_settings']['file_upload'] = array(
        '#type' => 'fieldset',
        '#title' => t('Image picker'),
        '#collapsible' => TRUE,
        '#collapsed' => (isset($user->imagepicker_advanced_collapsed) ? $user->imagepicker_advanced_collapsed : variable_get('imagepicker_advanced_collapsed', 0)),
        '#weight' => $weight,
      );
      $form['block_settings']['file_upload']['mpframe'] = array(
        '#type' => 'markup',
        '#value' => '<div id="imgp_tb">'. $iurl .'</div>',
      );
      $form['block_settings']['#prefix'] = '<a name="body_hash"></a>'. $form['block_settings']['#prefix'];
    }
  }
}

2) Added the following form element to imagepicker_settings_form() (in imagepicker.admin.inc):

  $form['imagepicker_settings']['image_picker_custom_forms'] = array(
    '#type' => 'textarea',
	'#title' => t('Custom Forms'),
	'#description' => t('Enter the form ID of any custom forms you want the image picker added to, one per line.'),
	'#default_value' => variable_get('image_picker_custom_forms', ''),
  );

This creates a textarea on the settings page that allows for a list of form_ids to be added for any forms that should have the imagepicker added. The imagepicker is then added to the form in question (though in the wrong spot - but I figure that's something that can be dealt with later). I'm not too sure on where to go from here though.

jaypan’s picture

I looked through this a bunch more - I ran into a brick wall (for today). In order to insert the data into the textfield where the cursor lies, the javascript needs to know the text area where the curosor lies. This can be done through a process of elimination when the imagepicker can only be used in node forms, comments and blocks, but when extended to any other form, it's harder to do. I haven't figured out a solution yet.

chrisschaub’s picture

I wonder if it would be possible to load imagepicker in a modal popup from editors like fck or ckeditor. So, an imagepicker button. That way, you'd have the instance, popup a modal box and have the imagepicker controls in there. Maybe this exists already?

hutch’s picture

The current dev version of imagepicker can be run in a colorbox
I don't know how you would get that into a WYSIWYG editor.
The link to it would be something like
"/imagepicker?width=700px&height=600&iframe=true" class="colorbox-load"

hutch’s picture

Status: Active » Closed (works as designed)
jaypan’s picture

It's your choice to close this, but it would be nice if the functionality could be provided in the future.

hutch’s picture

If you know how, provide a patch ;-)

jaypan’s picture

I got 2/3 of the way earlier in the thread. I'll have to look at it again sometime in the future. I'm deep into three projects right now, so it's not going to happen in the next month or so.

hutch’s picture

Status: Closed (works as designed) » Active

OK

avpaderno’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

I am closing this issue, since Drupal 4.x, 5.x, and 6.x are now not supported.