Drupal 7 upload Sub Logo Advanced Theme Settings

In many cases you might need to upload a second logo for a theme. In this case you would like to upload a second logo from admin.
You will need to add this code in your theme-settings.php page. (Remember to replace ‘yourtheme’ with the name of your current Drupal theme).

function yourtheme_form_system_theme_settings_alter(&$form, &$form_state) {

  $form['logo']['settings']['sublogo'] = array(
    '#type' => 'fieldset',
    '#title' => t('Second logo'),
    '#description' => t("Second logo for your page")
  );
 
    $logo_path = theme_get_setting('sublogo_path');
    // If $logo_path is a public:// URI, display the path relative to the files
    // directory; stream wrappers are not end-user friendly.
    if (file_uri_scheme($logo_path) == 'public') {
      $logo_path = file_uri_target($logo_path);
    }
    
  $form['logo']['settings']['sublogo']['sublogo_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path to second logo'),
    '#default_value' =>  $logo_path,
  );

  $form['logo']['settings']['sublogo']['sublogo_upload'] = array(
    '#type' => 'file',
    '#title' => t('Upload second logo'),
  );
  $form['#submit'][]   = 'yourtheme_settings_submit';
}

function yourtheme_settings_submit($form, &$form_state) {
  $settings = array();
  
  // Check for a new uploaded file, and use that if available.
  if ($file = file_save_upload('sublogo_upload')) {
    $parts = pathinfo($file->filename);
    $destination = 'public://' . $parts['basename'];
    $file->status = FILE_STATUS_PERMANENT;
    
  
     if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
        $_POST['sublogo_path'] = $form_state['values']['sublogo_path'] = $destination;
     }
  }
}

In admin you will have a look this this:

sublogo in Drupal

For front-end display of sub logo you will need this code –

$sub_logo = '';          
if($file_create_url(theme_get_setting('sublogo_path')))
{
		$sub_logo = file_create_url(variable_get('file_public_path', conf_path() . '/files/').theme_get_setting('sublogo_path'));
       ?>
          <img src="<?php echo $sub_logo; ?>" alt="" />
       <?php
}

Ta da! You have successfully created a place to dynamically upload a sub logo from admin. And learnt Drupal 7 upload Sub Logo advanced theme settings.

Subscribe
Notify of
guest
2 Comments
Inline Feedbacks
View all comments
2
0
Would love your thoughts, please comment.x
()
x