block_class.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Implements hook_permission().
  4. */
  5. function block_class_permission() {
  6. return array(
  7. 'administer block classes' => array(
  8. 'title' => t('Administer block classes'),
  9. 'description' => t('Set CSS classes for blocks.'),
  10. ),
  11. );
  12. }
  13. /*
  14. * Extend block's classes with any user defined classes.
  15. */
  16. function block_class_preprocess_block(&$vars) {
  17. $block = $vars['block'];
  18. /* The original code: */
  19. //$classes = block_class($block);
  20. //$vars['classes_array'] = array_merge($vars['classes_array'], explode(' ', $classes));
  21. /* Replaced with the following so it doesn't fire that many queries if you have a lot of blocks */
  22. static $_block_class_blocks_classes;
  23. if (! isset ($_block_class_blocks_classes)) {
  24. $_block_class_blocks_classes = array();
  25. $result = db_query('SELECT css_class, module, delta FROM {block_class}');
  26. while ($record = $result->fetchObject()) {
  27. $_block_class_blocks_classes[$record->module][$record->delta] = $record->css_class;
  28. }
  29. }
  30. if (isset($_block_class_blocks_classes[$block->module][$block->delta])) {
  31. $classes = $_block_class_blocks_classes[$block->module][$block->delta];
  32. $vars['classes_array'] = array_merge($vars['classes_array'], explode(' ', $classes));
  33. }
  34. }
  35. /*
  36. * Extend panel block's classes with any user defined classes.
  37. * Implements hook_preprocess_hook()
  38. */
  39. function block_class_preprocess_panels_pane(&$vars) {
  40. if ($vars['pane']->type != 'block') {
  41. return;
  42. }
  43. // Infer the block's $module and $delta from the pane subtype.
  44. $block_parts = explode('-', $vars['pane']->subtype);
  45. // Load the block based on the block parts.
  46. $block = block_load($block_parts[0], $block_parts[1]);
  47. // Return block_class classes as string.
  48. $css_class = block_class($block);
  49. // Add a generic 'module type' pane class.
  50. $vars['classes_array'][] = drupal_html_class('pane-' . $block->module);
  51. // Add $css_class to the $classes_array.
  52. $vars['classes_array'][] = $css_class;
  53. }
  54. /**
  55. * Return classes as string
  56. */
  57. function block_class($block) {
  58. $ret = db_query('SELECT css_class FROM {block_class} WHERE module = :module AND delta = :delta', array(':module' => $block->module, ':delta' => $block->delta))->fetchField();
  59. return $ret ? check_plain ($ret) : '';
  60. }
  61. /**
  62. * Alter block edit form
  63. */
  64. function block_class_form_alter(&$form, &$form_state, $form_id) {
  65. if (user_access('administer block classes') && $form_id == 'block_admin_configure' || $form_id == 'block_add_block_form') {
  66. $block = new stdClass;
  67. $block->module = $form['module']['#value'];
  68. $block->delta = $form['delta']['#value'];
  69. $css_class = block_class($block);
  70. // Create a more technical description for users with administer blocks permission.
  71. $description = t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.');
  72. $form['settings']['css_class'] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('CSS class(es)'),
  75. '#default_value' => $css_class,
  76. '#description' => t('Separate classes with a space.'),
  77. '#maxlength' => 255,
  78. );
  79. $form['#submit'][] = 'block_class_form_submit';
  80. }
  81. }
  82. /**
  83. * Save supplied class.
  84. */
  85. function block_class_form_submit($form, &$form_state) {
  86. if ($form_state['values']['form_id'] == 'block_admin_configure' || $form_state['values']['form_id'] == 'block_add_block_form') {
  87. if (isset($form_state['values']['css_class']) && user_access('administer blocks')) {
  88. $module = $form_state['values']['module'];
  89. $delta = $form_state['values']['delta'];
  90. $class = $form_state['values']['css_class'];
  91. db_delete('block_class')->condition('module', $module)->condition('delta', $delta)->execute();
  92. if (!empty($class)) {
  93. $id = db_insert('block_class')->fields(array('module' => $module, 'delta' => $delta, 'css_class' => $class))->execute();
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Implements hook_features_api().
  100. */
  101. function block_class_features_api() {
  102. return array(
  103. 'block_class' => array(
  104. 'name' => t('Block class'),
  105. 'feature_source' => TRUE,
  106. 'default_hook' => 'block_class_features_default_class',
  107. 'file' => drupal_get_path('module', 'block_class') . '/block_class.features.inc',
  108. ),
  109. );
  110. }