0

To begin I worked with this solution but a wild error appear: Image Upload in a Custom Module

Here is my code :

function module_top_content_equipe_admin_form(){
  $default = get_variable_value(variable_get('module_top_content_equipe_display', ''));
  $default_image = variable_get('module_top_content_equipe_image', '');
  $form['module_top_content_equipe_display'] = array(
    '#type' => 'text_format',
    '#name' => 'module_top_content_equipe_display',
    '#title' => t('Content of the block'),
    '#description' => t("Text to display"),
    '#format' => 'filtered_html',
    '#default_value' => $default,
  );
  $form['module_top_content_equipe_image'] = array(
    '#type' => 'managed_file',
    '#name' => 'module_top_content_equipe_image',
    '#title' => t('Top Content page l equipe Image'),
    '#description' => t("Image should be less than 400 pixels wide and in JPG format."),
    '#default_value' => $default_image,
    '#upload_location' => 'public://',
  );
  return system_settings_form($form);
}

function module_top_content_equipe_admin_form_submit($form, &$form_state){
  if (isset($form_state['values']['module_top_content_equipe_image'])) {
    $file = file_load($form_state['values']['module_top_content_equipe_image']);

    $file->status = FILE_STATUS_PERMANENT;

    file_save($file);
  }  
}

and my error when I submit the form

Fatal error: Cannot unset string offsets in /includes/form.inc on line 2157

Thank you with advance

1

There's a bug in the way system_settings_form() handles managed_file types. It's to do with the upload/remove buttons and parent elements (if you read the whole issue you hould get a good take on what's going on).

I don't know if it's the same error as I was having, but adding code like the following to the top of the form's submit handler fixed it for me:

foreach($form_state['buttons'] as $delta => &$button) {
  if (preg_match('/module_top_content_equipe_image_(upload|remove)_button/i', $button['#name'])) {
      $button['#parents'] = array();
    }
  }

Not ideal by any means, but it does work.

On another note, the #process property attached to the managed_file item is unnecessary. That code should be run in the form's submit handler, and since your submit handler is named in the right convention it will be run when the form is submitted, which is when you need it to.

Try removing the #process line altogether and see what happens.

|improve this answer|||||
  • Thank you Clive ! but it didn't work for me. I omit to precise that I get my errors when I load my form and so I can't submit it. – cocoggu Jul 12 '12 at 12:43
  • 1
    Ah ok...well you were going to run into this one next so hopefully it's saved happening that at least! I think the original problem is definitely related to the #process property, I'll update the answer – Clive Jul 12 '12 at 12:48
  • It's ok I remove #process line and I can access my form but when submitting, I have a very similar error than the bug you talk about : Fatal error: Cannot unset string offsets in /includes/form.inc on line 2157 . I tried to add your piece of code at the top of my submit function but it still the same. Thank you again for support – cocoggu Jul 12 '12 at 12:55
  • I update my answer – cocoggu Jul 12 '12 at 13:06
0

I success to submit my form only by adding this :

$form['#submit'][] = 'module_top_content_equipe_admin_form_submit';

before

return system_settings_form($form);

So as Clive say in it's first comment it's a bug between the managed_file #type and the system_settings_form() function. Just have to submit manually before this function and it works like a charm !

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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