6

I wrote the following module to upload a background image. My problem is, that my submit function is not called, though the pic is uploaded and showed in the block until the next cleanup is coming.The managed file table shows status 0...what should be 1 as far as i know.

Here is the Code of my Module: Whats the mistake?

function backgroundimage_block_info() {
    // This example comes from node.module.
    $blocks['backgroundimage'] = array(
        'info' => t('Background Image'),
    );


    return $blocks;
}
function backgroundimage_block_view($delta = '') {
    $fid_now=variable_get('backgroundimage');
    dpm($fid_now);
    $file_obj = file_load($fid_now);

    $bg_img_url = file_create_url($file_obj->uri);

    // This example is adapted from node.module.
    $block = array();

    switch ($delta) {
        case 'backgroundimage':
            $block['subject'] ="imagebackgroundblock";
            $block['content'] ="<div class='background-image'><img src='".$bg_img_url."'></div>";

            break;


    }
    return $block;
}
function backgroundimage_admin() {
    $form = array();

    $form['backgroundimage'] = array(
        '#type' => 'managed_file',
        '#name' => 'backgroundimage_image',
        '#title' => t('upload image, huh!'),
        '#default_value' => variable_get('backgroundimage', ''),
        '#description' => t("Here you can upload an image to the background!"),
        '#required' => TRUE,
        '#upload_location' => 'public://backgroundimage/'
    );

    return system_settings_form($form);
}
function backgroundimage_form_submit($form, $form_state) {
    dpm("yeah"); // is not being called!
    $form['backgroundimage']['#file']->status = FILE_STATUS_PERMANENT;
    file_save($form['backgroundimage']['#file']);
}

function backgroundimage_menu() {

    $items = array();

    $items['admin/settings/backgroundimage'] = array(
        'title' => 'Settings Background Image',
        'description' => 'Image upload',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('backgroundimage_admin'),
        'access arguments' => array('administer backgroundimage settings'),
        'type' => MENU_NORMAL_ITEM,
    );

    return $items;
}

?> 

Thanks a lot!

0
8

You have used system_settings_form_submit() so you can add custom submit handler for your form by adding this code to your hook_menu

function backgroundimage_admin() {
    $form = array();

    $form['backgroundimage'] = array(
        '#type' => 'managed_file',
        '#name' => 'backgroundimage_image',
        '#title' => t('upload image, huh!'),
        '#default_value' => variable_get('backgroundimage', ''),
        '#description' => t("Here you can upload an image to the background!"),
        '#required' => TRUE,
        '#upload_location' => 'public://backgroundimage/'
    );
    $form['#submit'][] = 'backgroundimage_form_submit';
    return system_settings_form($form);

}
6
  • thanks noek, somehow the function is still not called ;( – Jurudocs Apr 9 '13 at 7:39
  • updated my answer – Petro Popelyshko Apr 9 '13 at 7:48
  • the code after a return statment isnt executed ;) ...but the hint leads in the right direction! it supposed to be $form['#submit'][] = 'backgroundimage_form_submit'; return system_settings_form($form); – Jurudocs Apr 9 '13 at 7:56
  • i modified your answer so that its correct! – Jurudocs Apr 9 '13 at 8:00
  • yep , its just an example custom _callback – Petro Popelyshko Apr 9 '13 at 8:01

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.