While I found this module is very useful, one major flaw it is lacking the user friendly feature to upload image.

I made this code to give the module a standard uploading form element for both the menu images and menu hover images

I apologize if I cannot make proper patch for this.

change for imagemenu.admin.inc.

// changes for imagemenu_edit_item(&$form_state, $type, $item, $menu)

// alter the code
 if (module_exists('imce')) {
  $form['menu']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $item['title'],
    '#description' => t('The name of the menu item (displayed when hovering over a menu image).'),
    '#required' => TRUE,
    '#weight' => -10,
  );
  $form['menu']['imagepath'] = array(
    '#type' => 'textfield',
    '#title' => t('Image Path'),
    '#default_value' => $item['imagepath'],
    '#description' => t('The path to the image.'),
    '#required' => TRUE,
    '#weight' => -9,
  );
  $form['menu']['mouseover'] = array(
    '#type' => 'textfield',
    '#title' => t('Mouseover image path'),
    '#default_value' => $item['mouseover'],
    '#description' => t('Optional. The path to an image to display on mouseover.'),
    '#weight' => -8,
  );
 
    $form['menu']['imagepath']['#description'] = t('The path to the image. ').'<input type="button" value="Browse" 
      onClick="window.open(\'/?q=imce&app=imagemenu|url%40edit-menu-imagepath\', \'\', \'width=760,height=560,resizable=1\')"';
    $form['menu']['mouseover']['#description'] = t('The path to the image. ').'<input type="button" value="Browse" 
      onClick="window.open(\'/?q=imce&app=imagemenu|url%40edit-menu-mouseover\', \'\', \'width=760,height=560,resizable=1\')"';
  } else {
    $form['menu']['#tree'] = FALSE;
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    $form['menu']['imagepath'] = array(
    '#type' => 'file',
    '#title' => t('Upload Image'),
    '#description' => t('Upload image for this menu item'),
    '#weight' => -9,
		'#size' => 10,
  );
	
	
  $form['menu']['mouseover'] = array(
    '#type' => 'file',
    '#title' => t('Upload mouseover image'),
    '#description' => t('Optional. Upload image for this menu item mouseover'),
    '#weight' => -8,
		'#size' => 10,
  );
  }
// changes for imagemenu_edit_item_validate($form, &$form_state)
if (!module_exists('imce')) {
  // validate the allowed extension
	$validators_file = array('file_validate_extensions' => array('gif jpeg jpg png'));
  // Save the image menu
	$directory = file_directory_path() .'/imagemenu';
	file_check_directory($directory, $mode = 1, $form_item = NULL);
  if ($file = file_save_upload('imagepath',  $validators_file, $directory, FILE_EXISTS_REPLACE)) {
			file_set_status($file, 1);
			$form_state['values']['menu']['imagepath'] = $file->filepath;
  } else {
  	form_set_error('', t('Image file is required'));
	}	
	// save the mouseover
	if ($file = file_save_upload('mouseover', $validators_file, $directory, FILE_EXISTS_REPLACE)) {
			file_set_status($file, 1);
			$form_state['values']['menu']['mouseover'] = $file->filepath;
  }
}	
	
  $item = &$form_state['values']['menu'];
  // if the path begins with a slash strip it off
  if (substr($item['imagepath'], 0, 1) == '/') {
    $item['imagepath'] = substr($item['imagepath'], 1);
  }
  $check = is_readable($item['imagepath']);
  if (!$check) form_set_error('imagepath', t('File not found.'));
  $check = is_readable($item['mouseover']);
  if (!$check && $item['mouseover']) form_set_error('mouseover', t('File not found.'));

the code above is tested marginally and it need community to make proper patch and rigorously tested it.

Comments

duckzland’s picture

EDIT :
because we eliminate the #tree for making the upload element works the following code is necessary addition


// addtional code because of #tree is set to false
$form_state['values']['menu'] = $form_state['values'];
// end of additional code

// changes for imagemenu_edit_item_validate($form, &$form_state)
if (!module_exists('imce')) {
  // validate the allowed extension
    $validators_file = array('file_validate_extensions' => array('gif jpeg jpg png'));
  // Save the image menu
    $directory = file_directory_path() .'/imagemenu';
    file_check_directory($directory, $mode = 1, $form_item = NULL);
  if ($file = file_save_upload('imagepath',  $validators_file, $directory, FILE_EXISTS_REPLACE)) {
            file_set_status($file, 1);
            $form_state['values']['menu']['imagepath'] = $file->filepath;
  } else {
      form_set_error('', t('Image file is required'));
    }   
    // save the mouseover
    if ($file = file_save_upload('mouseover', $validators_file, $directory, FILE_EXISTS_REPLACE)) {
            file_set_status($file, 1);
            $form_state['values']['menu']['mouseover'] = $file->filepath;
  }
}   
   
  $item = &$form_state['values']['menu'];
  // if the path begins with a slash strip it off
  if (substr($item['imagepath'], 0, 1) == '/') {
    $item['imagepath'] = substr($item['imagepath'], 1);
  }
  $check = is_readable($item['imagepath']);
  if (!$check) form_set_error('imagepath', t('File not found.'));
  $check = is_readable($item['mouseover']);
  if (!$check && $item['mouseover']) form_set_error('mouseover', t('File not found.'));

I don't know for sure if we really need #tree? because it is usually used for form with ahah?

pobster’s picture

Status: Active » Closed (outdated)

Closing as D6.x is now unsupported.