-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayout_custom_theme.module
120 lines (108 loc) · 3.65 KB
/
layout_custom_theme.module
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* @file
* Main file for Layout Custom Theme module.
*/
/**
* Implements hook_config_info().
*/
function layout_custom_theme_config_info() {
$prefixes['layout_custom_theme.settings'] = array(
'label' => t('Layout Custom Theme settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_form_FORM_ID_alter() for layout_settings_form.
*/
function layout_custom_theme_form_layout_settings_form_alter(&$form, &$form_state, $form_id) {
// Get the layout object being modified.
$layout = $form_state['layout'];
// Get the config item if it exists.
$config = config_get('layout_custom_theme.settings', $layout->name);
// Define the form elements.
$form['custom_theme'] = array(
'#title' => t('Custom Theme'),
'#type' => 'item',
'#access' => !$layout->isDefault(),
);
$form['custom_theme']['enable'] = array(
'#type' => 'checkbox',
'#title' => t('Use custom theme on this layout.'),
'#default_value' => (empty($config)) ? FALSE : TRUE,
'#access' => !$layout->isDefault(),
);
// Get the options for the themes.
$options = layout_custom_theme_get_themes();
$form['custom_theme']['theme'] = array(
'#type' => 'select',
'#title' => 'Custom theme',
'#options' => $options,
'#empty_option' => '- Select theme -',
'#default_value' => (empty($config)) ? '' : $config,
'#states' => array(
'visible' => array(':input[name="custom_theme[enable]"]' => array('checked' => TRUE)),
'required' => array(':input[name="custom_theme[enable]"]' => array('checked' => TRUE)),
),
'#access' => !$layout->isDefault(),
);
$form['actions']['submit']['#submit'][] = 'layout_custom_theme_form_layout_settings_form_alter_submit';
}
/**
* Helper function to get custom theme options.
*
* @return array
* An array of all enabled, non-hidden themes with machine name and label.
*/
function layout_custom_theme_get_themes() {
// Get current list of themes.
$themes = system_rebuild_theme_data();
// Define array to collect options.
$custom_theme_options = array();
// Loop through all themes.
foreach ($themes as $theme) {
// Check if enabled and not hidden.
if ($theme->status && empty($theme->info['hidden'])) {
// Add to array using machine name as key and label as value.
$custom_theme_options[$theme->name] = $theme->info['name'];
}
}
return $custom_theme_options;
}
/**
* Save the settings.
*/
function layout_custom_theme_form_layout_settings_form_alter_submit($form, &$form_state) {
// Get the layout object being modified.
$layout = $form_state['layout'];
// Define the config file.
$config_name = 'layout_custom_theme.settings';
// Check if custom theme is enabled on the form.
if ($form_state['values']['custom_theme']['enable']) {
// If so, set a config item for this layout with the value of this theme.
config_set($config_name, $layout->name, $form_state['values']['custom_theme']['theme']);
}
else {
// If not, delete the config item if it exists.
config_clear($config_name, $layout->name);
}
}
/**
* Implements hook_custom_theme().
*/
function layout_custom_theme_custom_theme() {
// Get the current item.
$item = menu_get_item();
// Check that an item has been returned and that user has access to it.
if ($item && $item['access']) {
// Get the layout in use by the current item.
$layout = layout_get_layout_by_path(NULL, $item);
// Attempt to retrieve a custom theme settings for this layout.
$theme = config_get('layout_custom_theme.settings', $layout->name);
// If a setting is returned, define this as the custom theme.
if ($theme) {
return $theme;
}
}
}