I'm trying to add a 'managed_file' form element to a custom content type that I'm using with Panels. I've added the following to my 'masthead_content_type_edit_form' within the plugin include file 'masthead_content_type.inc'

  // Use the #managed_file FAPI element to upload an image file.
  $form['content_image'] = array(
    '#title' => t('Image'),
    '#type' => 'managed_file',
    '#description' => t('Image file'),
    '#default_value' => !empty($conf['content_image']) ? $conf['content_image'] : '',
    '#upload_location' => 'public://masthead_images/',
    '#array_parents' => array('content_image')
  );

However when I upload the file I receive the following error messages:

Notice: Undefined index: masthead_content_type_edit_form in drupal_retrieve_form() (line 736 of /trunk/includes/form.inc).
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'masthead_content_type_edit_form' was given in drupal_retrieve_form() (line 771 of /trunk/includes/form.inc).
Notice: Undefined index: content_image in file_ajax_upload() (line 265 of /trunk/modules/file/file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 274 of /trunk/modules/file/file.module).

Having looked through the file module and form include I can see that the AHAH callback is unable fails to find the form function using function_exists().

Does anyone have any advice on whether a) the image upload can be used within a plugin and b) what needs to be updated or changed to make the form upload function correctly.

Comments

merlinofchaos’s picture

Status: Active » Fixed

You'll need to use form_load_include() and provide the proper information to ensure your file gets loaded.

See http://api.drupal.org/api/drupal/includes--form.inc/function/form_load_i...

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

squiggy’s picture

I am trying to do the same thing in a custom content type -- only with a form field type of 'file', instead of a 'managed_file'. I did not encounter the error initially reported here, but I still am unable to get this to work.

@merlinofchaos, I did try to use form_load_include() to include modules/file.field.inc, but that didn't seem to help. Is there a different file that should be included?

In my case, when I upload a file in the pane settings of a custom content type that I created, then click Finish, the form hangs indefinitely and does not close the modal. Then, if I click to close the modal and Save the panel, it *does* save the changes. At no point are there any PHP or ajax error messages. Any clues as to how to get this working would be very much appreciated.

Here is the content type edit, validate and submit functions I am using. The field in question is $form['sidebar_title_image_image_upload'], but I am including the other fields just in case it's related.

function my_module_sidebar_content_free_content_type_edit_form($form, &$form_state) {
  $conf = $form_state['conf'];
  // Title text
  $form['sidebar_content_free_title'] = array(
    '#title' => t('Title'),
    '#type' => 'textfield',
    '#size' => 45,
	  '#description' => t('If you don\'t upload a title image below, this text will display as the title. If you do upload a title image, this text will be the alt tag for that image.'),
    '#default_value' => ($conf['sidebar_content_free_title']) ? $conf['sidebar_content_free_title'] : '',
  );
  // Body text
  $form['sidebar_content_free_text']['body'] = array(
    '#title' => t('Content'),
    '#type' => 'text_format',
    '#default_value' => $conf['body']['value'],
    '#format' => filter_fallback_format(),
  );
  // If an image has been uploaded already, display a preview of it above the upload field. 
  if ($conf['sidebar_title_image_image_fid']) {
    $fid = $conf['sidebar_title_image_image_fid'];
    $results = db_query("SELECT * FROM {file_managed} WHERE fid = :fid", array(':fid' => $fid));
    foreach ($results as $file_data) {
      $file = $file_data;
    }
    $variables = array(
      'path' => $file->uri, 
      'alt' => $conf['sidebar_content_free_title'],
      'title' => $conf['sidebar_title_image_title'],
      'attributes' => array('class' => 'title-image'),
    );
    $image = theme('image', $variables);
    $form['sidebar_title_image_image_display'] = array(
      '#markup' => '<div>'. $image .'</div>',
    );
  }
  // Image title 
  $form['sidebar_title_image_image_upload'] = array(
    '#title' => ($conf['sidebar_title_image_image_fid']) ? t('Upload an image for the title banner') : t('Title Image'),
    '#type' => 'file',
    '#array_parents' => array('sidebar_title_image_image_upload'),
  );
  // Field ID of previously uploaded image
  $form['sidebar_title_image_image_fid'] = array(
    '#type' => 'hidden',
    '#value' => ($conf['sidebar_title_image_image_fid']) ? $conf['sidebar_title_image_image_fid'] : NULL,
  );
  
  $form['#attributes']['enctype'] = "multipart/form-data";
  
  return $form;
}

/** 
* Validate form.
*/
function my_module_sidebar_content_free_content_type_edit_form_validate($form, &$form_state) {
 // Validator basically taken from examples module
 $file = file_save_upload('sidebar_title_image_image_upload', array());
  // If the file passed validation:
  if ($file) {
    // Move the file, into the Drupal file system
    if ($file = file_move($file, 'public://')) {
      // Save the file for use in the submit handler.
      $form_state['storage']['sidebar_title_image_image_upload'] = $file;
    }
    else {
      form_set_error('sidebar_title_image_image_upload', t('Failed to write the image to the site\'s file folder.'));
    }
  }
  else {
    form_set_error('sidebar_title_image_image_upload', t('No file was uploaded.'));
  }
}

function my_module_sidebar_content_free_content_type_edit_form_submit($form, &$form_state) {
  $file = $form_state['storage']['sidebar_title_image_image_upload'];
  // We are done with the file, remove it from storage.
  unset($form_state['storage']['sidebar_title_image_image_upload']);
  // Make the storage of the file permanent
  $file->status = FILE_STATUS_PERMANENT;
  // Save file status.
  file_save($file);
  // Set a response to the user.
  drupal_set_message(t('Saved image'));

  // Set value of previously uploaded image
  $form_state['values']['sidebar_title_image_image_fid'] = $file->fid;

  // This automatically saves all the values passed to it in the $conf array
  foreach($form_state['values'] as $value => $value_object) {
    $form_state['conf'][$value] = $value_object;
  }
}

I started off upgrading from a drupal 6 implementation, but switched out code form the examples module for the validation/submit piece to try to rule out any errors on my part.

Also, if I comment out the image upload related code and the enctype line, the other fields can be edited and saved without any glitches. If I comment out all but the image upload field, it still does not work as I explained.

Apologies for posting so many lines of code; but I didn't want to leave out any potentially relevant info. Thanks again for a second set of eyes on this. :)

squiggy’s picture

Title: managed_file form element in content types » managed_file or file form element in content types

Updating the title to reflect this scenario with a file form element too.

squiggy’s picture

Ok, the Panels Media module has a working example of a file upload within a ctools custom content type: https://drupal.org/project/panels_media. I will try that.

tthenne’s picture

Version: 7.x-1.x-dev » 7.x-1.2
Status: Closed (fixed) » Active
Issue tags: +ctools plugins, +managed_field

I'm reopening this even though it has been awhile...I'm updating the ctools version to 7.x-1.2 as I believe that is the stable release now. I'm still getting the same error as @openbook in the original post:

Notice: Undefined index: content_image_pane_edit_form in drupal_retrieve_form() (line 763 of /htdocs/development/includes/form.inc).
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'content_image_pane_edit_form' was given in drupal_retrieve_form() (line 798 of /htdocs/development/includes/form.inc).
Notice: Undefined index: pane_image in file_ajax_upload() (line 271 of /htdocs/development/modules/file/file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 280 of /htdocs/development/modules/file/file.module).

The file upload field displays correctly, but after selecting the file (in this case an image) and clicking the upload button, I receive the error as shown above. As suggested by @merlinofchaos I included the form_load_include() function and defined the parameters so that it would include the file.field.inc from the file module. I confirmed this by printing the path ( form_load_include() returns the path) and it displays the correct path to the file.field.inc file. I currently have the form_load_include() in the 'Edit Form' callback of my $plugin array. The documentation for the form_load_include() states:

Use this function instead of module_load_include() from inside a form constructor or any form processing logic as it ensures that the include file is loaded whenever the form is processed. In contrast to using module_load_include() directly, form_load_include() makes sure the include file is correctly loaded also if the form is cached.

I would assume the the 'Edit Form' callback as declared in the $plugin array would classify as a 'form constructor', but may be presumptuous, and could be wrong. Can anyone confirm that the form_load_include() is in an appropriate spot.

Also, what I find strange is that it is uploading the file (the file is uploaded to the 'public://location/' folder as defined in the '#upload_location' attribute of the $form['image_field']. Could anyone shine light on why I'm still seeing the error as shown above and as originally described by @openbook?

Thanks in advance,

TH

joel_osc’s picture

Answer is in this post: #1600934: Node edit form in content_type edit form

Just add this to the top of your edit form:

form_load_include($form_state, 'inc', 'my_module', 'plugins/content_types/myfile');

And everything will work!

mustanggb’s picture

Status: Active » Closed (outdated)