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
Comment #1
merlinofchaos commentedYou'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...
Comment #3
squiggy commentedI 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.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
enctypeline, 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. :)
Comment #4
squiggy commentedUpdating the title to reflect this scenario with a file form element too.
Comment #5
squiggy commentedOk, 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.
Comment #6
tthenne commentedI'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:
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 theform_load_include()in the 'Edit Form' callback of my $plugin array. The documentation for theform_load_include()states: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
Comment #7
joel_osc commentedAnswer 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!
Comment #8
mustanggb commented