block_class.module 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @file
  4. * Enhanced control over the CSS Classes of any Block.
  5. *
  6. * Block Class allows users to add classes to any block through the block's
  7. * configuration interface. This implementation is based on an alteration of
  8. * the Core block database table to leverage the Core Block API functions,
  9. * objects and structure.
  10. */
  11. /**
  12. * Implements hook_permission().
  13. */
  14. function block_class_permission() {
  15. return array(
  16. 'administer block classes' => array(
  17. 'title' => t('Administer block classes'),
  18. 'description' => t('Set CSS classes for blocks.'),
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements theme_preprocess_block().
  24. *
  25. * Extend block's classes with any user defined classes.
  26. */
  27. function block_class_preprocess_block(&$vars) {
  28. $block = $vars['block'];
  29. if (!empty($block->css_class)) {
  30. $classes_array = explode(' ', $block->css_class);
  31. foreach ($classes_array as $class) {
  32. $vars['classes_array'][] = drupal_clean_css_identifier($class, array());
  33. }
  34. }
  35. }
  36. /**
  37. * Implements hook_preprocess_HOOK().
  38. *
  39. * Extend panel block's classes with any user defined classes.
  40. */
  41. function block_class_preprocess_panels_pane(&$vars) {
  42. if ($vars['pane']->type != 'block') {
  43. return;
  44. }
  45. // Infer the block's $module and $delta from the pane subtype.
  46. $block_parts = explode('-', $vars['pane']->subtype);
  47. // Load the block based on the block parts.
  48. $block = block_load($block_parts[0], $block_parts[1]);
  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. if (!empty($block->css_class)) {
  53. $classes_array = explode(' ', $block->css_class);
  54. foreach ($classes_array as $class) {
  55. $vars['classes_array'][] = drupal_clean_css_identifier($class, array());
  56. }
  57. }
  58. }
  59. /**
  60. * Implements hook_form_alter().
  61. *
  62. * Alter block edit form to add configuration field.
  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. // Load statically cached block object used to display the form.
  67. $block = block_load($form['module']['#value'], $form['delta']['#value']);
  68. $form['settings']['css_class'] = array(
  69. '#type' => 'textfield',
  70. '#title' => t('CSS class(es)'),
  71. '#default_value' => isset($block->css_class) ? $block->css_class : '',
  72. '#description' => t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.'),
  73. '#maxlength' => 255,
  74. );
  75. $form['#submit'][] = 'block_class_form_submit';
  76. }
  77. }
  78. /**
  79. * Helper function: additional submit callback for block configuration pages.
  80. *
  81. * Save supplied CSS classes.
  82. */
  83. function block_class_form_submit($form, &$form_state) {
  84. if ($form_state['values']['form_id'] == 'block_admin_configure' || $form_state['values']['form_id'] == 'block_add_block_form') {
  85. // Only save if value has changed.
  86. if (isset($form_state['values']['css_class']) && $form['settings']['css_class']['#default_value'] != $form_state['values']['css_class'] && user_access('administer blocks')) {
  87. db_update('block')
  88. ->fields(array('css_class' => $form_state['values']['css_class']))
  89. ->condition('module', $form_state['values']['module'])
  90. ->condition('delta', $form_state['values']['delta'])
  91. ->execute();
  92. // Flush all context module cache to use the updated css_class.
  93. if (module_exists('context')) {
  94. cache_clear_all('context', 'cache', TRUE);
  95. }
  96. }
  97. }
  98. }