forked from GetDKAN/nuboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-settings.php
77 lines (74 loc) · 2.47 KB
/
theme-settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* @file
* Theme setting callbacks for the nuboot theme.
*/
/**
* Implements hook_form_system_theme_settings_alter().
*/
function nuboot_form_system_theme_settings_alter(&$form, &$form_state) {
// Hero fieldset.
$form['hero'] = array(
'#type' => 'fieldset',
'#title' => t('Hero Unit'),
'#group' => 'general',
);
// Default path for image.
$hero_path = theme_get_setting('hero_path');
if (file_uri_scheme($hero_path) == 'public') {
$hero_path = file_uri_target($hero_path);
}
// Helpful text showing the file name, non-editable.
$form['hero']['hero_path'] = array(
'#type' => 'textfield',
'#title' => 'Path to front page hero unit background image',
'#default_value' => $hero_path,
'#disabled' => TRUE,
);
// Upload field.
$form['hero']['hero_upload'] = array(
'#type' => 'file',
'#title' => 'Upload a photo for the hero unit background image',
'#description' => 'Upload a new image for the hero region background.',
'#upload_validators' => array(
'file_validate_extensions' => array('png jpg jpeg'),
),
);
// Attach custom submit handler to the form.
$form['#submit'][] = 'nuboot_settings_submit';
}
/**
* Implements hook_setings_submit().
*/
function nuboot_settings_submit($form, &$form_state) {
$settings = array();
// If the user entered a path relative to the system files directory for
// for the hero unit, store a public:// URI so the theme system can handle it.
if (!empty($values['hero_path'])) {
$values['hero_path'] = _system_theme_settings_validate_path($values['hero_path']);
}
// Get the previous value.
$previous = $form['hero']['hero_path']['#default_value'];
if ($previous !== 'profiles/dkan/themes/contrib/nuboot/images/hero.jpg') {
$previous = 'public://' . $previous;
}
else {
$previous = FALSE;
}
if ($file = file_save_upload('hero_upload')) {
$parts = pathinfo($file->filename);
$destination = 'public://' . $parts['basename'];
$file->status = FILE_STATUS_PERMANENT;
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
$_POST['hero_path'] = $form_state['values']['hero_path'] = $destination;
// If new file has a different name than the old one, delete the old.
if ($previous && $destination != $previous) {
drupal_unlink($previous);
}
}
}
else {
// Avoid error when the form is submitted without specifying a new image.
$_POST['hero_path'] = $form_state['values']['hero_path'] = $previous;
}
}