1

In Drupal 7, I am trying to put an image field to allow an image to be uploaded to the theme settings.

I am using managed file from the form API to do this. Here is my code:

  $form['footer']['footer_logo'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
  '#default_value' => variable_get('footer_logo', ''),
  '#upload_location' => 'public://footer_logo/',
);

When I call the image using theme_get_setting('footer_logo'), I just get a set of numbers. How can I get the file path of the image instead? Also, is there away to pass the image through image cache settings.

  • Where are you using that code? – kiamlaluno Jan 4 '13 at 13:10
  • @kiamlaluno Isn't that the right code to use to allow images to be uploaded in theme settings? – big_smile Jan 4 '13 at 16:15
  • I was just wondering if that code is used in a module, or in the theme-settings.php file of a theme. – kiamlaluno Jan 4 '13 at 17:06
  • @kiamlaluno I am using the code in my theme-settings file of a theme. Thanks for any help you can offer! – big_smile Jan 4 '13 at 17:14
  • @kiamlaluno I wonder, how could I upload multiple files to the theme settings?! Suppose I want to set images for a slider from the settings. – SaidbakR Jul 20 '14 at 18:31
3

It seems that this could provide an answer to your question :

$fid = theme_get_setting('footer_logo');
$image_url = file_create_url(file_load($fid)->uri);

(but I didn't test it).

0

Here's a bullet proof version to get you the image URL.

Basically, it does all the checks at each function call to be 100% sure you get a proper URL and also to not throw any errors in case the image isn't uploaded or the current theme doesn't have this setting available.

When done, $file_url is either an empty string or a valid URL in a string.

Good luck.

$file_url = '';
$file_fid = theme_get_setting('footer_logo');
if(!empty($file_fid)) {
    $file_loaded = file_load($file_fid);
    if($file_loaded !== false) {
        if(!empty($file_loaded->uri)) {
            $file_url_raw = file_create_url($file_loaded->uri);
            if($file_url_raw !== false) {
                $file_url = $file_url_raw;
            }
        }
    }
}

In addition, the way you're saving the image files they will be set to temporary only. Check my answer here for more info on how to solve this: https://drupal.stackexchange.com/a/136911/4861

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.