dr:themesettings
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dr:themesettings [2007/05/13 07:03] – warning about official project fgm | dr:themesettings [2020/11/23 17:23] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Adding settings on Drupal themes ====== | ||
+ | ===== WARNING ===== | ||
+ | A module called [[http:// | ||
+ | |||
+ | ===== Themes need settings ===== | ||
+ | |||
+ | For some time now, I've been feeling the need to have some themes be more easily customizable by admins, without their needing to tweak files. Alas, the themeing system is normally limited to the smallest common denominator defined by < | ||
+ | |||
+ | Since tweaking the various themeing mechanisms seemed complicated, | ||
+ | |||
+ | As the first step, we'll add new settings to a theme, by declaring a < | ||
+ | |||
+ | ===== Adding settingability to themes ===== | ||
+ | ==== Creating the settings function for theme " | ||
+ | |||
+ | In the admin.theme file, we'll add the admin_settings function. If the theme is a plain PHP theme, it will already exist. If it is a PHPtemplate theme, as in the case of our " | ||
+ | <code php> | ||
+ | function admin_settings() | ||
+ | { | ||
+ | $title = t(' | ||
+ | drupal_set_title($title); | ||
+ | $form = array(); | ||
+ | |||
+ | $form[' | ||
+ | ( | ||
+ | '# | ||
+ | '# | ||
+ | '# | ||
+ | '# | ||
+ | ); | ||
+ | return system_settings_form(' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Proof of concept example, no more: we just define a setting for the theme to use later on. In this case, it will be a CSS background color. NOTICE: a true world example should not take input and store it without some sanity checks. | ||
+ | |||
+ | ==== Using the setting: node.tpl.php ==== | ||
+ | |||
+ | With the " | ||
+ | <code php> | ||
+ | <?php | ||
+ | echo '< | ||
+ | . variable_get(' | ||
+ | . ' ! important"' | ||
+ | . ' | ||
+ | . '">' | ||
+ | if ($page == 0) ... | ||
+ | [...the rest of the file is unchanged...] | ||
+ | </ | ||
+ | |||
+ | Ugly inline styling, but this is just a demo, so we don't care. I add the "! important" | ||
+ | |||
+ | ===== Hooking it up ===== | ||
+ | |||
+ | At this stage, we obviously have a color-parametered node.tpl.php, | ||
+ | |||
+ | We now need to tell Drupal to stitch this together, and for this we'll need a module. | ||
+ | |||
+ | ==== The themesettings module ==== | ||
+ | |||
+ | <code php> | ||
+ | // themesettings.module for Drupal 4.7 | ||
+ | // (C) 2006 FG Marand | ||
+ | // Licensed under the Cecill 2.0 license http:// | ||
+ | // | ||
+ | define(TSBASEPATH, | ||
+ | |||
+ | function _themesettings_themes() | ||
+ | { | ||
+ | static $ret ; | ||
+ | |||
+ | if (isset($ret)) | ||
+ | { return $ret; } | ||
+ | |||
+ | $ret = array(); | ||
+ | $themes = list_themes(); | ||
+ | foreach ($themes as $theme_key => $theme) | ||
+ | { | ||
+ | $function_name = $theme_key . " | ||
+ | // Test taken from system.module, | ||
+ | // We need to load the file for the function to be in memory, though | ||
+ | // Having it in a separate file avoids loading the full theme | ||
+ | if (file_exists($file = dirname($theme-> | ||
+ | { | ||
+ | include_once " | ||
+ | } | ||
+ | if (function_exists($function_name)) | ||
+ | { | ||
+ | $ret[$theme_key] = $theme; | ||
+ | } | ||
+ | } | ||
+ | return $ret; | ||
+ | } | ||
+ | |||
+ | function themesettings_help($section) | ||
+ | { | ||
+ | switch ($section) | ||
+ | { | ||
+ | case ' | ||
+ | $ret = ' | ||
+ | break; | ||
+ | case ' | ||
+ | $ret = t(' | ||
+ | break; | ||
+ | } | ||
+ | return $ret; | ||
+ | } | ||
+ | |||
+ | |||
+ | function themesettings_menu($may_cache) | ||
+ | { | ||
+ | $items = array(); | ||
+ | |||
+ | if (!$may_cache) | ||
+ | { | ||
+ | $access = user_access(' | ||
+ | |||
+ | $items[] = array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | foreach (_themesettings_themes() as $theme_name => $theme) | ||
+ | { | ||
+ | $items[] = array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | } | ||
+ | } | ||
+ | return $items; | ||
+ | } | ||
+ | |||
+ | function themesettings_page() | ||
+ | { | ||
+ | $ret = t('< | ||
+ | $links = array(); | ||
+ | foreach (_themesettings_themes() as $theme_name => $theme_name) | ||
+ | { | ||
+ | $links[] = l($theme_name, | ||
+ | } | ||
+ | $ret .= theme(' | ||
+ | return $ret; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== How does it work ? ===== | ||
+ | |||
+ | There' | ||
+ | |||
+ | * it registers a set of paths in the admin area by implementing hook_menu | ||
+ | * to know which themes implement this non-standard extension, it needs to know whether the < | ||
+ | * for PHPtemplate themes, the .theme file won't be loaded except for admin/ | ||
+ | * for plain PHP themes, the .theme file is already loaded anyway, so parsing this function only adds a few CPU cycles, but no additional file inclusion, so the additional load remains minimal too. | ||
+ | |||
+ | Please try it and comment on [[http:// |