Revision c2f02e8f
Added by Andreas Kohlbecker over 10 years ago
7.x/themes/zen_dataportal/theme-settings.php | ||
---|---|---|
1 | 1 |
<?php |
2 |
|
|
3 |
/** |
|
4 |
* Provides the image names which can be configured in the theme settings to all |
|
5 |
* theme setting functions. |
|
6 |
* |
|
7 |
* @return multitype:string |
|
8 |
* the list of image names which can be configured in the theme settings |
|
9 |
*/ |
|
10 |
function _zen_dataportal_imagenames() { |
|
11 |
static $image_list = array('banner', 'body_background', 'page_background'); |
|
12 |
return $image_list; |
|
13 |
} |
|
14 |
|
|
2 | 15 |
/** |
3 | 16 |
* Implements hook_form_system_theme_settings_alter(). |
4 | 17 |
* |
... | ... | |
13 | 26 |
return; |
14 | 27 |
} |
15 | 28 |
|
16 |
// Create the form using Forms API: http://api.drupal.org/api/7 |
|
17 |
|
|
18 |
$which_image = "banner"; |
|
19 |
$form['zen_dataportal'] = array( |
|
20 |
'#type' => 'fieldset', |
|
21 |
'#title' => t($which_image) . ' ' . t('image settings'), |
|
22 |
'#description' => t('If toggled on, the following image will be displayed.'), |
|
23 |
'#attributes' => array('class' => array('theme-settings-bottom')), |
|
24 |
); |
|
25 |
$form['zen_dataportal']['default_' . $which_image] = array( |
|
26 |
'#type' => 'checkbox', |
|
27 |
'#title' => t('Use the default') . t($which_image), |
|
28 |
'#default_value' => theme_get_setting('default_' . $which_image), |
|
29 |
'#tree' => FALSE, |
|
30 |
'#description' => t('Check here if you want the theme to use the image supplied with it.') |
|
31 |
); |
|
32 |
$form['zen_dataportal']['settings'] = array( |
|
33 |
'#type' => 'container', |
|
34 |
'#states' => array( |
|
35 |
// Hide the logo settings when using the default logo. |
|
36 |
'invisible' => array( |
|
37 |
'input[name="default_' . $which_image . '"]' => array('checked' => TRUE), |
|
29 |
$form['zen_dataportal_colors'] = array( |
|
30 |
'#type' => 'fieldset', |
|
31 |
'#title' => t('Colors'), |
|
32 |
'#description' => t('Configure colors where.'), |
|
33 |
'#attributes' => array('class' => array('theme-settings-bottom')), |
|
34 |
); |
|
35 |
$form['zen_dataportal_colors']['site_name_color'] = array( |
|
36 |
'#type' => 'textfield', |
|
37 |
'#title' => t('Site name color'), |
|
38 |
'#default_value' => theme_get_setting('site_name_color'), |
|
39 |
'#description' => t('Set the color of the site name which is shown in the header. Must be a css color value like: #000000'), |
|
40 |
); |
|
41 |
|
|
42 |
// |
|
43 |
// custom images for banner, body and page backgrounds |
|
44 |
// |
|
45 |
foreach(_zen_dataportal_imagenames() as $which_image) { |
|
46 |
$image_label = str_replace('_', ' ', $which_image); |
|
47 |
$form['zen_dataportal_' . $which_image] = array( |
|
48 |
'#type' => 'fieldset', |
|
49 |
'#title' => ucfirst(t($image_label)) . ' ' . t('image settings'), |
|
50 |
'#description' => t('If toggled on, the following image will be displayed.'), |
|
51 |
'#attributes' => array('class' => array('theme-settings-bottom')), |
|
52 |
); |
|
53 |
$form['zen_dataportal_' . $which_image]['default_' . $which_image] = array( |
|
54 |
'#type' => 'checkbox', |
|
55 |
'#title' => t('Use the default') . t($which_image), |
|
56 |
'#default_value' => theme_get_setting('default_' . $which_image), |
|
57 |
'#tree' => FALSE, |
|
58 |
'#description' => t('Check here if you want the theme to use the image supplied with it.') |
|
59 |
); |
|
60 |
$form['zen_dataportal_' . $which_image]['settings'] = array( |
|
61 |
'#type' => 'container', |
|
62 |
'#states' => array( |
|
63 |
// Hide the logo settings when using the default logo. |
|
64 |
'invisible' => array( |
|
65 |
'input[name="default_' . $which_image . '"]' => array('checked' => TRUE), |
|
66 |
), |
|
38 | 67 |
), |
39 |
), |
|
40 |
); |
|
41 |
|
|
42 |
// If $path is a public:// URI, display the path relative to the files |
|
43 |
// directory; stream wrappers are not end-user friendly. |
|
44 |
$path = theme_get_setting($which_image . '_path'); |
|
45 |
$image = ''; |
|
46 |
if (file_uri_scheme($path) == 'public') { |
|
47 |
$url = file_create_url($path); |
|
48 |
$path = file_uri_target($path); |
|
49 |
$form['zen_dataportal']['settings'][$which_image . '_preview'] = array( |
|
50 |
'#type' => 'item', |
|
51 |
'#title' => t('Preview'), |
|
52 |
'#markup' => '<div class="image-preview"><img src="' . $url . '"/></div>', |
|
53 | 68 |
); |
54 |
} |
|
55 |
$form['zen_dataportal']['settings'][$which_image . '_path'] = array( |
|
56 |
'#type' => 'textfield', |
|
57 |
'#title' => t('Path to custom') . ' ' . t($which_image), |
|
58 |
'#description' => t('The path to the file you would like to use as your') . ' ' . t($which_image), |
|
59 |
'#default_value' => $path, |
|
60 |
); |
|
61 |
$form['zen_dataportal']['settings'][$which_image . '_upload'] = array( |
|
62 |
'#type' => 'file', |
|
63 |
'#title' => t('Upload') . ' ' . t($which_image), |
|
64 |
'#maxlength' => 40, |
|
65 |
'#description' => t("If you don't have direct file access to the server, use this field to upload your image.") |
|
66 |
); |
|
69 |
|
|
70 |
// If $path is a public:// URI, display the path relative to the files |
|
71 |
// directory; stream wrappers are not end-user friendly. |
|
72 |
$path = theme_get_setting($which_image . '_path'); |
|
73 |
$image = ''; |
|
74 |
if (file_uri_scheme($path) == 'public') { |
|
75 |
$url = file_create_url($path); |
|
76 |
$path = file_uri_target($path); |
|
77 |
$form['zen_dataportal_' . $which_image]['settings'][$which_image . '_preview'] = array( |
|
78 |
'#type' => 'item', |
|
79 |
'#title' => t('Preview'), |
|
80 |
'#markup' => '<div class="image-preview"><img src="' . $url . '"/></div>', |
|
81 |
); |
|
82 |
} |
|
83 |
$form['zen_dataportal_' . $which_image]['settings'][$which_image . '_path'] = array( |
|
84 |
'#type' => 'textfield', |
|
85 |
'#title' => t('Path to custom') . ' ' . t($image_label), |
|
86 |
'#description' => t('The path to the file you would like to use as your') . ' ' . t($image_label), |
|
87 |
'#default_value' => $path, |
|
88 |
); |
|
89 |
$form['zen_dataportal_' . $which_image]['settings'][$which_image . '_upload'] = array( |
|
90 |
'#type' => 'file', |
|
91 |
'#title' => t('Upload') . ' ' . t($image_label) . t(' image'), |
|
92 |
'#maxlength' => 40, |
|
93 |
'#description' => t("If you don't have direct file access to the server, use this field to upload your image.") |
|
94 |
); |
|
95 |
} // END of loop over image names |
|
67 | 96 |
|
68 | 97 |
|
69 | 98 |
$form['#validate'][] = 'zen_dataportal_theme_settings_validate'; |
... | ... | |
75 | 104 |
|
76 | 105 |
/** |
77 | 106 |
* Process zen_dataportal admin form submissions. |
107 |
* @param $form |
|
108 |
* Nested array of form elements that comprise the form. |
|
109 |
* @param $form_state |
|
110 |
* A keyed array containing the current state of the form. |
|
78 | 111 |
*/ |
79 | 112 |
function zen_dataportal_theme_settings_submit($form, &$form_state) { |
80 | 113 |
|
81 | 114 |
$values = &$form_state['values']; |
82 | 115 |
|
83 |
// If the user uploaded a new image, save it to a permanent location |
|
84 |
// and use it in place of the default theme-provided file. |
|
85 |
$which_image = "banner"; |
|
86 |
if ($file = $values[$which_image . '_upload']) { |
|
87 |
unset($values[$which_image . '_upload']); |
|
88 |
$filename = file_unmanaged_copy($file->uri); |
|
89 |
$values['default_' .$which_image ] = 0; |
|
90 |
$values[$which_image . '_path'] = $filename; |
|
91 |
$values['toggle_' . $which_image] = 1; |
|
92 |
} |
|
93 |
if (!empty($values[$which_image . '_path'])) { |
|
94 |
$values[$which_image . '_path'] = _system_theme_settings_validate_path($values[$which_image . '_path']); |
|
95 |
} |
|
116 |
foreach(_zen_dataportal_imagenames() as $which_image) { |
|
117 |
// If the user uploaded a new image, save it to a permanent location |
|
118 |
// and use it in place of the default theme-provided file. |
|
119 |
if ($file = $values[$which_image . '_upload']) { |
|
120 |
unset($values[$which_image . '_upload']); |
|
121 |
$filename = file_unmanaged_copy($file->uri); |
|
122 |
$values['default_' .$which_image ] = 0; |
|
123 |
$values[$which_image . '_path'] = $filename; |
|
124 |
$values['toggle_' . $which_image] = 1; |
|
125 |
} |
|
126 |
if (!empty($values[$which_image . '_path'])) { |
|
127 |
$values[$which_image . '_path'] = _system_theme_settings_validate_path($values[$which_image . '_path']); |
|
128 |
} |
|
129 |
|
|
130 |
} // END of loop over image names |
|
96 | 131 |
|
97 | 132 |
/* |
98 | 133 |
* Ok, we are done here the $values will be saved in the theme |
99 | 134 |
* variable by system_theme_settings_submit($form, &$form_state) |
135 |
* in modules/system/system.admin.inc |
|
100 | 136 |
*/ |
101 | 137 |
|
102 | 138 |
} |
103 | 139 |
|
104 | 140 |
/** |
105 | 141 |
* Validator for the system_theme_settings() form. |
142 |
* @param $form |
|
143 |
* Nested array of form elements that comprise the form. |
|
144 |
* @param $form_state |
|
145 |
* A keyed array containing the current state of the form. |
|
106 | 146 |
*/ |
107 | 147 |
function zen_dataportal_theme_settings_validate($form, &$form_state) { |
108 | 148 |
|
109 | 149 |
$validators = array('file_validate_is_image' => array()); |
110 | 150 |
|
111 |
$which_image = "banner"; |
|
112 |
// Check for a new uploaded logo. |
|
113 |
$file = file_save_upload($which_image . '_upload', $validators); |
|
114 |
if (isset($file)) { |
|
115 |
// File upload was attempted. |
|
116 |
if ($file) { |
|
117 |
// Put the temporary file in form_values so we can save it on submit. |
|
118 |
$form_state['values'][$which_image . '_upload'] = $file; |
|
119 |
} |
|
120 |
else { |
|
121 |
// File upload failed. |
|
122 |
form_set_error($which_image . '_upload', t('The image could not be uploaded.')); |
|
151 |
foreach(_zen_dataportal_imagenames() as $which_image) { |
|
152 |
// Check for a new uploaded logo. |
|
153 |
$file = file_save_upload($which_image . '_upload', $validators); |
|
154 |
if (isset($file)) { |
|
155 |
// File upload was attempted. |
|
156 |
if ($file) { |
|
157 |
// Put the temporary file in form_values so we can save it on submit. |
|
158 |
$form_state['values'][$which_image . '_upload'] = $file; |
|
159 |
} |
|
160 |
else { |
|
161 |
// File upload failed. |
|
162 |
form_set_error($which_image . '_upload', t('The image could not be uploaded.')); |
|
163 |
} |
|
123 | 164 |
} |
124 |
} |
|
165 |
} // END of loop over image names
|
|
125 | 166 |
} |
Also available in: Unified diff
colors configurable