View source
<?php
define('CLEAN_MARKUP_BLOCK_NO_ELEMENT', 'none');
function clean_markup_blocks_permission() {
$permissions = array();
$permissions['administer clean markup block settings'] = array(
'title' => t('Administer clean block markup settings'),
'description' => t('Change the HTML used to render blocks.'),
);
return $permissions;
}
function clean_markup_blocks_form_alter(&$form, &$form_state, $form_id) {
if ($form_id === 'block_admin_configure' || $form_id == 'block_add_block_form') {
if (user_access('administer clean markup block settings')) {
$defaults = variable_get('clean_markup_blocks-defaults');
$wrapper_elements = _clean_markup_get_html_wrapper_elements();
$optional_wrapper_elements = _clean_markup_get_html_wrapper_elements(TRUE);
$variable_name = _clean_markup_blocks_generate_prefix($form['module']['#value'], $form['delta']['#value']);
$this_block_settings = variable_get($variable_name, $defaults);
$form['#submit'][] = '_clean_markup_blocks_block_configure_submit';
$form['clean_markup'] = array(
'#type' => 'fieldset',
'#title' => t('Clean markup options'),
'#group' => 'visibility',
);
$form['clean_markup']['block_wrapper'] = array(
'#type' => 'select',
'#title' => t('Block wrapper markup'),
'#description' => t('Choose the HTML element to wrap the block.'),
'#default_value' => $this_block_settings['block_wrapper'],
'#options' => $optional_wrapper_elements,
);
$form['clean_markup']['enable_inner_div'] = array(
'#type' => 'checkbox',
'#title' => t('Enable inner div'),
'#description' => t('Specify if you want an inner div element inside the main block wrapper.'),
'#default_value' => $this_block_settings['enable_inner_div'],
'#states' => array(
'invisible' => array(
':input[name="block_wrapper"]' => array(
'value' => CLEAN_MARKUP_BLOCK_NO_ELEMENT,
),
),
),
);
$form['clean_markup']['additional_block_classes'] = array(
'#type' => 'textfield',
'#title' => t('Additional block classes'),
'#description' => t('Additional classes to set on the block wrapper.'),
'#default_value' => $this_block_settings['additional_block_classes'],
);
$form['clean_markup']['block_html_id_as_class'] = array(
'#type' => 'checkbox',
'#title' => t("Output block's HTML ID as class"),
'#default_value' => $this_block_settings['block_html_id_as_class'],
);
$form['clean_markup']['title_wrapper'] = array(
'#type' => 'select',
'#title' => t('Title wrapper markup'),
'#description' => t('Choose the HTML to use to wrap the block title.'),
'#default_value' => $this_block_settings['title_wrapper'],
'#options' => $wrapper_elements,
);
$form['clean_markup']['title_hide'] = array(
'#type' => 'checkbox',
'#title' => t('Visually-hide block title'),
'#description' => t('Add the <code>element-invisible</code> CSS class to the block title. This hides it visually but leaves it visible to screenreaders.'),
'#default_value' => $this_block_settings['title_hide'],
);
$form['clean_markup']['content_wrapper'] = array(
'#type' => 'select',
'#title' => t('Content wrapper markup'),
'#description' => t('Choose the HTML to use to wrap the block content.'),
'#default_value' => $this_block_settings['content_wrapper'],
'#options' => $optional_wrapper_elements,
);
}
}
}
function _clean_markup_blocks_block_configure_submit($form, &$form_state) {
if (user_access('administer clean markup block settings')) {
$defaults = variable_get('clean_markup_blocks-defaults');
$valid_wrapper_elements = _clean_markup_get_html_wrapper_elements(TRUE);
$variable_name = _clean_markup_blocks_generate_prefix($form_state['values']['module'], $form_state['values']['delta']);
$new_block_settings = variable_get($variable_name, $defaults);
$new_block_settings['block_wrapper'] = array_key_exists($form_state['values']['block_wrapper'], $valid_wrapper_elements) ? $form_state['values']['block_wrapper'] : $defaults['block_wrapper'];
$new_block_settings['title_wrapper'] = array_key_exists($form_state['values']['title_wrapper'], $valid_wrapper_elements) ? $form_state['values']['title_wrapper'] : $defaults['title_wrapper'];
$new_block_settings['content_wrapper'] = array_key_exists($form_state['values']['content_wrapper'], $valid_wrapper_elements) ? $form_state['values']['content_wrapper'] : $defaults['content_wrapper'];
$new_block_settings['additional_block_classes'] = $form_state['values']['additional_block_classes'];
$new_block_settings['enable_inner_div'] = (bool) $form_state['values']['enable_inner_div'];
$new_block_settings['title_hide'] = (bool) $form_state['values']['title_hide'];
variable_set($variable_name, $new_block_settings);
}
}
function _clean_markup_blocks_generate_prefix($module, $delta) {
return 'clean_markup_blocks--' . $module . '-' . $delta;
}
function clean_markup_blocks_preprocess_block(&$variables) {
$defaults = variable_get('clean_markup_blocks-defaults');
$variable_name = _clean_markup_blocks_generate_prefix($variables['block']->module, $variables['block']->delta);
$current_block_settings = variable_get($variable_name, $defaults);
$variables['block_wrapper'] = $current_block_settings['block_wrapper'];
$variables['inner_div'] = $current_block_settings['enable_inner_div'];
$variables['classes_array'] = array_merge($variables['classes_array'], explode(' ', check_plain($current_block_settings['additional_block_classes'])));
if ($variables['block']->subject) {
$variables['title'] = array(
'#prefix' => render($variables['title_prefix']),
'#suffix' => render($variables['title_suffix']),
'#type' => 'html_tag',
'#tag' => $current_block_settings['title_wrapper'],
'#attributes' => isset($variables['title_attributes']) ? $variables['title_attributes'] : array(),
'#value' => $variables['block']->subject,
);
$variables['title']['#attributes']['class'] = array(
'title',
'block-title',
);
if ($current_block_settings['title_hide']) {
$variables['title']['#attributes']['class'][] = 'element-invisible';
}
}
else {
$variables['title'] = array();
}
if ($current_block_settings['content_wrapper'] !== CLEAN_MARKUP_BLOCK_NO_ELEMENT) {
$variables['content'] = array(
'#type' => 'html_tag',
'#tag' => $current_block_settings['content_wrapper'],
'#attributes' => array(
'class' => array(
'content',
),
),
'#value' => $variables['content'],
);
$variables['content']['#attributes'] += $variables['content_attributes_array'];
}
if (array_key_exists('block_html_id_as_class', $current_block_settings) && $current_block_settings['block_html_id_as_class'] == TRUE) {
$variables['classes_array'][] = $variables['block_html_id'];
}
}
function clean_markup_blocks_theme_registry_alter(&$theme_registry) {
$theme_registry['block']['template'] = drupal_get_path('module', 'clean_markup_blocks') . '/theme/block';
}