You are here

function system_theme_settings in Drupal 6

Same name and namespace in other branches
  1. 4 modules/system.module \system_theme_settings()
  2. 5 modules/system/system.module \system_theme_settings()
  3. 7 modules/system/system.admin.inc \system_theme_settings()

Form builder; display theme configuration for entire site and individual themes.

Parameters

$key: A theme name.

Return value

The form structure.

See also

system_theme_settings_submit()

Related topics

2 string references to 'system_theme_settings'
color_form_alter in modules/color/color.module
Implementation of hook_form_alter().
system_menu in modules/system/system.module
Implementation of hook_menu().

File

modules/system/system.admin.inc, line 334
Admin page callbacks for the system module.

Code

function system_theme_settings(&$form_state, $key = '') {
  $directory_path = file_directory_path();
  file_check_directory($directory_path, FILE_CREATE_DIRECTORY, 'file_directory_path');

  // Default settings are defined in theme_get_settings() in includes/theme.inc
  if ($key) {
    $settings = theme_get_settings($key);
    $var = str_replace('/', '_', 'theme_' . $key . '_settings');
    $themes = system_theme_data();
    $features = $themes[$key]->info['features'];
  }
  else {
    $settings = theme_get_settings('');
    $var = 'theme_settings';
  }
  $form['var'] = array(
    '#type' => 'hidden',
    '#value' => $var,
  );

  // Check for a new uploaded logo, and use that instead.
  if ($file = file_save_upload('logo_upload', array(
    'file_validate_is_image' => array(),
  ))) {
    $parts = pathinfo($file->filename);
    $filename = $key ? str_replace('/', '_', $key) . '_logo.' . $parts['extension'] : 'logo.' . $parts['extension'];

    // The image was saved using file_save_upload() and was added to the
    // files table as a temporary file. We'll make a copy and let the garbage
    // collector delete the original upload.
    if (file_copy($file, $filename, FILE_EXISTS_REPLACE)) {
      $_POST['default_logo'] = 0;
      $_POST['logo_path'] = $file->filepath;
      $_POST['toggle_logo'] = 1;
    }
  }

  // Check for a new uploaded favicon, and use that instead.
  if ($file = file_save_upload('favicon_upload')) {
    $parts = pathinfo($file->filename);
    $filename = $key ? str_replace('/', '_', $key) . '_favicon.' . $parts['extension'] : 'favicon.' . $parts['extension'];

    // The image was saved using file_save_upload() and was added to the
    // files table as a temporary file. We'll make a copy and let the garbage
    // collector delete the original upload.
    if (file_copy($file, $filename)) {
      $_POST['default_favicon'] = 0;
      $_POST['favicon_path'] = $file->filepath;
      $_POST['toggle_favicon'] = 1;
    }
  }

  // Toggle settings
  $toggles = array(
    'logo' => t('Logo'),
    'name' => t('Site name'),
    'slogan' => t('Site slogan'),
    'mission' => t('Mission statement'),
    'node_user_picture' => t('User pictures in posts'),
    'comment_user_picture' => t('User pictures in comments'),
    'search' => t('Search box'),
    'favicon' => t('Shortcut icon'),
    'primary_links' => t('Primary links'),
    'secondary_links' => t('Secondary links'),
  );

  // Some features are not always available
  $disabled = array();
  if (!variable_get('user_pictures', 0)) {
    $disabled['toggle_node_user_picture'] = TRUE;
    $disabled['toggle_comment_user_picture'] = TRUE;
  }
  if (!module_exists('search')) {
    $disabled['toggle_search'] = TRUE;
  }
  $form['theme_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Toggle display'),
    '#description' => t('Enable or disable the display of certain page elements.'),
  );
  foreach ($toggles as $name => $title) {
    if (!$key || in_array($name, $features)) {
      $form['theme_settings']['toggle_' . $name] = array(
        '#type' => 'checkbox',
        '#title' => $title,
        '#default_value' => $settings['toggle_' . $name],
      );

      // Disable checkboxes for features not supported in the current configuration.
      if (isset($disabled['toggle_' . $name])) {
        $form['theme_settings']['toggle_' . $name]['#disabled'] = TRUE;
      }
    }
  }

  // System wide only settings.
  if (!$key) {

    // Create neat 2-column layout for the toggles
    $form['theme_settings'] += array(
      '#prefix' => '<div class="theme-settings-left">',
      '#suffix' => '</div>',
    );

    // Toggle node display.
    $node_types = node_get_types('names');
    if ($node_types) {
      $form['node_info'] = array(
        '#type' => 'fieldset',
        '#title' => t('Display post information on'),
        '#description' => t('Enable or disable the <em>submitted by Username on date</em> text when displaying posts of the following type.'),
        '#prefix' => '<div class="theme-settings-right">',
        '#suffix' => '</div>',
      );
      foreach ($node_types as $type => $name) {
        $form['node_info']["toggle_node_info_{$type}"] = array(
          '#type' => 'checkbox',
          '#title' => check_plain($name),
          '#default_value' => $settings["toggle_node_info_{$type}"],
        );
      }
    }
  }
  elseif (!element_children($form['theme_settings'])) {

    // If there is no element in the theme settings fieldset then do not show
    // it -- but keep it in the form if another module wants to alter.
    $form['theme_settings']['#access'] = FALSE;
  }

  // Logo settings
  if (!$key || in_array('logo', $features)) {
    $form['logo'] = array(
      '#type' => 'fieldset',
      '#title' => t('Logo image settings'),
      '#description' => t('If toggled on, the following logo will be displayed.'),
      '#attributes' => array(
        'class' => 'theme-settings-bottom',
      ),
    );
    $form['logo']["default_logo"] = array(
      '#type' => 'checkbox',
      '#title' => t('Use the default logo'),
      '#default_value' => $settings['default_logo'],
      '#tree' => FALSE,
      '#description' => t('Check here if you want the theme to use the logo supplied with it.'),
    );
    $form['logo']['logo_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path to custom logo'),
      '#default_value' => $settings['logo_path'],
      '#description' => t('The path to the file you would like to use as your logo file instead of the default logo.'),
    );
    $form['logo']['logo_upload'] = array(
      '#type' => 'file',
      '#title' => t('Upload logo image'),
      '#maxlength' => 40,
      '#description' => t("If you don't have direct file access to the server, use this field to upload your logo."),
    );
  }
  if (!$key || in_array('favicon', $features)) {
    $form['favicon'] = array(
      '#type' => 'fieldset',
      '#title' => t('Shortcut icon settings'),
      '#description' => t("Your shortcut icon, or 'favicon', is displayed in the address bar and bookmarks of most browsers."),
    );
    $form['favicon']['default_favicon'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use the default shortcut icon.'),
      '#default_value' => $settings['default_favicon'],
      '#description' => t('Check here if you want the theme to use the default shortcut icon.'),
    );
    $form['favicon']['favicon_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path to custom icon'),
      '#default_value' => $settings['favicon_path'],
      '#description' => t('The path to the image file you would like to use as your custom shortcut icon.'),
    );
    $form['favicon']['favicon_upload'] = array(
      '#type' => 'file',
      '#title' => t('Upload icon image'),
      '#description' => t("If you don't have direct file access to the server, use this field to upload your shortcut icon."),
    );
  }
  if ($key) {

    // Call engine-specific settings.
    $function = $themes[$key]->prefix . '_engine_settings';
    if (function_exists($function)) {
      $group = $function($settings);
      if (!empty($group)) {
        $form['engine_specific'] = array(
          '#type' => 'fieldset',
          '#title' => t('Theme-engine-specific settings'),
          '#description' => t('These settings only exist for all the templates and styles based on the %engine theme engine.', array(
            '%engine' => $themes[$key]->prefix,
          )),
        );
        $form['engine_specific'] = array_merge($form['engine_specific'], $group);
      }
    }

    // Create a list which includes the current theme and all its base themes.
    if (isset($themes[$key]->base_themes)) {
      $theme_keys = array_keys($themes[$key]->base_themes);
      $theme_keys[] = $key;
    }
    else {
      $theme_keys = array(
        $key,
      );
    }

    // Process the theme and all its base themes.
    foreach ($theme_keys as $theme) {

      // Include the theme-settings.php file.
      $filename = './' . str_replace("/{$theme}.info", '', $themes[$theme]->filename) . '/theme-settings.php';
      if (file_exists($filename)) {
        require_once $filename;
      }
      $function = $theme . '_settings';
      if (!function_exists($function)) {
        $function = $themes[$theme]->prefix . '_settings';
      }
      if (function_exists($function)) {
        $group = $function($settings);
        if (!empty($group)) {
          $form['theme_specific']['#type'] = 'fieldset';
          $form['theme_specific']['#title'] = t('Theme-specific settings');
          $form['theme_specific']['#description'] = t('These settings only exist for the %theme theme and all the styles based on it.', array(
            '%theme' => $themes[$theme]->info['name'],
          ));
          $form['theme_specific'] = array_merge($form['theme_specific'], $group);
        }
      }
    }
  }
  $form['#attributes'] = array(
    'enctype' => 'multipart/form-data',
  );
  $form = system_settings_form($form);

  // We don't want to call system_settings_form_submit(), so change #submit.
  $form['#submit'] = array(
    'system_theme_settings_submit',
  );
  return $form;
}