You are here

public function sweaver_plugin_themesettings::sweaver_get_theme_settings_form in Sweaver 7

Same name and namespace in other branches
  1. 6 plugins/sweaver_plugin_themesettings/sweaver_plugin_themesettings.inc \sweaver_plugin_themesettings::sweaver_get_theme_settings_form()

Get theme settings form. We basically copy the form from system.admin.inc. This makes it easer to copy the uploads of the favicon & logo to revision versions of the style and getting the right settings for the theme.

People altering form and having also uploads wishing to work with sweaver must handle the revisioning themeselves.

Parameters

$key: The name of the theme.

$revision_theme_settings: Revisions theme settings.

1 call to sweaver_plugin_themesettings::sweaver_get_theme_settings_form()
sweaver_plugin_themesettings::sweaver_form in plugins/sweaver_plugin_themesettings/sweaver_plugin_themesettings.inc
Frontend form.

File

plugins/sweaver_plugin_themesettings/sweaver_plugin_themesettings.inc, line 296
Theme settings plugin.

Class

sweaver_plugin_themesettings
@file Theme settings plugin.

Code

public function sweaver_get_theme_settings_form($key, $revision_theme_settings) {
  $has_theme = FALSE;
  $form_state = array();
  $features = array();

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

  // Check for a new uploaded logo. Put it in the form
  // so we can give it the right filename in the submit function.
  if ($file = file_save_upload('logo_upload', array(
    'file_validate_is_image' => array(),
  ))) {
    $parts = pathinfo($file->uri);
    $filename = 'public://sweaver/temp_logo.' . $parts['extension'];
    $form['#logo_path'] = $filename;
    $form['#logo_path_extension'] = $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_unmanaged_copy($file->uri, $filename, FILE_EXISTS_REPLACE)) {
      $_POST['default_logo'] = 0;
      $_POST['logo_path'] = $file->uri;
      $_POST['toggle_logo'] = 1;
    }
  }

  // Check for a new uploaded favicon. Put it in the form
  // so we can give it the right filename in the submit function.
  if ($file = file_save_upload('favicon_upload')) {
    $parts = pathinfo($file->uri);
    $filename = 'public://sweaver/temp_favicon.' . $parts['extension'];
    $form['#favicon_path'] = $filename;
    $form['#favicon_path_extension'] = $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_unmanaged_copy($file->uri, $filename)) {
      $_POST['default_favicon'] = 0;
      $_POST['favicon_path'] = $file->uri;
      $_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' => check_plain($title),
        '#default_value' => $this
          ->sweaver_has_theme_setting('toggle_' . $name, $key, $revision_theme_settings),
      );

      // 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 (!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['sweaver_plugin_themesettings']['#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' => array(
          'theme-settings-bottom',
        ),
      ),
    );
    $form['logo']['default_logo'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use the default logo'),
      '#default_value' => $this
        ->sweaver_has_theme_setting('default_logo', $key, $revision_theme_settings),
      '#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' => $this
        ->sweaver_has_theme_setting('logo_path', $key, $revision_theme_settings),
      '#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' => $this
        ->sweaver_has_theme_setting('default_favicon', $key, $revision_theme_settings),
      '#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' => $this
        ->sweaver_has_theme_setting('favicon_path', $key, $revision_theme_settings),
      '#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 = $theme_info->prefix . '_engine_settings';
    if (function_exists($function)) {
      $form['engine_specific'] = array(
        '#type' => 'fieldset',
        '#title' => t('Theme-engine-specific settings'),
        '#description' => t('These settings only exist for the themes based on the %engine theme engine.', array(
          '%engine' => $themes[$key]->prefix,
        )),
      );
      $function($form, $form_state);
    }

    // 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,
      );
    }

    // Save the name of the current theme (if any), so that we can temporarily
    // override the current theme and allow theme_get_setting() to work
    // without having to pass the theme name to it.
    $default_theme = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : NULL;
    $GLOBALS['theme_key'] = $key;
    $other_theme_settings = array();

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

      // Include the theme-settings.php file.
      $filename = DRUPAL_ROOT . '/' . str_replace("/{$theme}.info", '', $theme_info->filename) . '/theme-settings.php';
      if (file_exists($filename)) {
        require_once $filename;
      }
      $function = $theme . '_form_system_theme_settings_alter';
      if (function_exists($function)) {
        $form_before = $form;
        $function($form, $form_state);
        $diff = array_diff_key($form, $form_before);
        $other_theme_settings = array_merge($other_theme_settings, $this
          ->sweaver_recursive_list_children($diff));
      }
    }
    $other_theme_settings_str = implode(',', $other_theme_settings);
    $form['other_theme_settings'] = array(
      '#type' => 'hidden',
      '#value' => $other_theme_settings_str,
    );

    // Restore the original current theme.
    if (!is_null($default_theme)) {
      $GLOBALS['theme_key'] = $default_theme;
    }
    else {
      unset($GLOBALS['theme_key']);
    }
  }
  return $form;
}