1

I was wondering if anyone has been successful with incorporating an Image field 'type'=>'managed_file' into a ctools plugin. I created a custom ctools plugin but am having some difficulty with the image field as I am getting an error upon upload. After some searching, it seems like this error could be caused from a file that isn't being included when the form is rebuilt after the ajax request. I've attempted to use the form_load_include to include the DRUPAL_ROOT/modules/file/file.field.inc file, and it is being loaded with the form, but I am still receiving this error:

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).

Here is my settings for my Custom Content Type Ctools Plugin:

/*Plugin definition*/
$plugin = array(
  'single' => TRUE,
  'title' => t('Custom: Content Image Pane'),
  'description' => t('This is a custom pane containing a title, textarea and image field.'),
  'category' => t('Custom Panes'),
  'edit form' => 'content_image_pane_edit_form',
  'render callback' => 'content_image_pane_render',
  'admin info' => 'content_image_pane_admin_info',
  'defaults' => array(
    'pane_image' => '',
    'pane_content' => '',
  )
);
/**
 * 'Edit form' callback for the content type.
 */
function content_image_pane_edit_form($form, &$form_state) {

  $form['#attributes']['enctype'] = array("multipart/form-data");

  form_load_include($form_state, 'inc', 'file', 'file.field');

  $conf = $form_state['conf'];

   $form['pane_image'] = array(
    '#title' => t('Image'),
    '#type' => 'managed_file',
    '#description' => t('Upload an Image that is associated with this content.'),
    '#default_value' => '',
    //'#default_value' => !empty($conf['pane_image']) ? variable_get($conf['pane_image']) : '',
    '#upload_location' => 'public://panes/',
    '#upload_validators' => array(
       'file_validate_extensions' => array('gif png jpeg jpg')
     ),
    '#array_parents' => array('pane_image')
  );

  $form['pane_content'] = array(
    '#title' => t('Content'),
    '#type' => 'text_format',
    '#format' => 'full_html',
    '#description' => t('Please enter the content for the custom pane here.'),
    '#default_value' => $conf['pane_content']['value'],
    '#cols' => 40,
    '#rows' => 6,
    '#resizable' => TRUE,
    '#required' => TRUE,
  );
  return $form;
}

If anyone could point me in the right direction for an answer to this that would be outstanding!

2 Answers 2

6

You should try to put this line:

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

at the top of your content_image_pane_edit_form function, replacing my_module and my_file with your module and plugin include file.

Ctools panes are generated in a separate modal from base Drupal forms and so when the AJAX reloads the form it cannot find the original form function. By forcing Drupal to include the full form code each time it is processed, the AJAX will be able to find the necessary function to rebuild the form.

Tested working with Drupal 7 & panes on a production site.

3
  • What does in edit form mean?
    – SeanJA
    May 13, 2014 at 20:06
  • It means inside the function content_image_pane_edit_form
    – jenlampton
    Nov 23, 2016 at 23:44
  • yes @JenLampton . Nov 28, 2016 at 14:04
1

If you still get the same error message, try move the form function into .module file.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.