less.theme.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * Contains alterations to theme settings forms.
  5. */
  6. /**
  7. * Implements hook_form_alter().
  8. */
  9. function less_form_alter(&$form, &$form_state, $form_id) {
  10. if ($form_id === 'system_theme_settings') {
  11. preg_match('/^theme_(?P<current_theme_key>\w+)_settings$/', $form['var']['#value'], $matches);
  12. // only show settings on forms for themes, not "Global settings"
  13. if (!empty($matches['current_theme_key'])) {
  14. $current_theme_key = $matches['current_theme_key'];
  15. $saved_settings = theme_get_setting('less', $current_theme_key);
  16. $less_settings_form = array();
  17. $theme_items = system_list('theme');
  18. if (isset($theme_items[$current_theme_key]) && !empty($theme_items[$current_theme_key]->info['less']['vars'])) {
  19. $less_settings_form[$current_theme_key] = _less_setting_form_element($current_theme_key, $theme_items[$current_theme_key]->info['less']['vars'], $saved_settings[$current_theme_key]);
  20. }
  21. // showing settings form for modules
  22. $system_items = system_list('module_enabled');
  23. $less_module_settings = array();
  24. foreach ($system_items as $system_item_name => $system_item) {
  25. if (!empty($system_item->info['less']['vars'])) {
  26. $item_saved_settings = isset($saved_settings[$system_item_name]) ? $saved_settings[$system_item_name] : array();
  27. $less_module_settings[$system_item_name] = _less_setting_form_element($system_item_name, $system_item->info['less']['vars'], $item_saved_settings, $system_item->info['name']);
  28. $less_module_settings[$system_item_name]['#group'] = 'less][modules_list';
  29. }
  30. }
  31. if (!empty($less_module_settings)) {
  32. $less_settings_form['modules'] = array(
  33. '#type' => 'fieldset',
  34. '#title' => t('Modules'),
  35. '#collapsible' => TRUE,
  36. '#collapsed' => TRUE,
  37. '#parents' => array('less'),
  38. // make sure all settings are stored under the system name immediately inside the themes less settings array
  39. );
  40. $less_settings_form['modules']['modules_list'] = array(
  41. '#type' => 'vertical_tabs',
  42. );
  43. $less_settings_form['modules'] += $less_module_settings;
  44. }
  45. if (!empty($less_settings_form)) {
  46. $form['less'] = array(
  47. '#type' => 'vertical_tabs',
  48. '#prefix' => t('<h3>LESS settings</h3>'),
  49. '#tree' => TRUE,
  50. '#description' => t('These settings are per theme. Delete a value to use the default.'),
  51. '#attached' => array(
  52. 'css' => array(
  53. drupal_get_path('module', 'less') . '/styles/less.theme.css',
  54. ),
  55. ),
  56. );
  57. $form['less'] += $less_settings_form;
  58. }
  59. }
  60. }
  61. }
  62. function _less_setting_form_element($system_name, $less_settings, $saved_settings, $title = NULL) {
  63. $form = array(
  64. '#type' => 'fieldset',
  65. '#title' => isset($title) ? $title : $system_name,
  66. '#attributes' => array(
  67. 'class' => array(
  68. 'less-theme-settings',
  69. ),
  70. ),
  71. );
  72. if (module_exists($system_name)) {
  73. $less_settings = array_replace($less_settings, (array) module_invoke($system_name, 'less_variables'));
  74. }
  75. foreach ($less_settings as $var_name => $var_value) {
  76. $form[$var_name] = array(
  77. '#type' => 'textfield',
  78. '#title' => $var_name,
  79. '#default_value' => !empty($saved_settings[$var_name]) ? $saved_settings[$var_name] : NULL,
  80. '#description' => t('Default value: %variable_value', array('%variable_value' => $var_value)),
  81. '#attributes' => array('placeholder' => $var_value),
  82. );
  83. }
  84. return $form;
  85. }