function _block_attributes_form_alter in Block Attributes 7
Add the block attributes fields to a block add or configuration form.
Parameters
array $form: The block's creation or configuration form passed by reference.
object $block: The optional existing block object for context.
1 call to _block_attributes_form_alter()
- block_attributes_form_alter in ./
block_attributes.module - Implements hook_form_alter().
File
- ./
block_attributes.module, line 253 - Enhanced control over the HTML attributes of any Block.
Code
function _block_attributes_form_alter(array &$form, $block) {
// Add the Block Attributes fieldsets in block's settings section before the
// regions and visibility sections.
// Fieldset to wrap fields related with block level attributes.
$form['settings']['options'][BLOCK_ATTRIBUTES_BLOCK] = array(
'#type' => 'fieldset',
'#title' => t('Block attributes'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
// The tree property is important, otherwise values will not be saved.
'#tree' => TRUE,
);
// Fieldset to wrap fields related with block title attributes.
$form['settings']['options'][BLOCK_ATTRIBUTES_TITLE] = array(
'#type' => 'fieldset',
'#title' => t('Block title attributes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
// Fieldset to wrap fields related with block content attributes.
$form['settings']['options'][BLOCK_ATTRIBUTES_CONTENT] = array(
'#type' => 'fieldset',
'#title' => t('Block content attributes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
// Hide the serialized value to be compared with new value upon submission
// even if it is NULL, it still needs to be defined.
$form['settings']['options']['serialized_attributes'] = array(
'#type' => 'hidden',
'#value' => isset($block->options) ? $block->options : NULL,
);
// Collect all Block Attributes defined in code.
$attributes = block_attributes_get_block_attribute_info();
// Unserialize and load an array containing all of block's attributes.
$block_options = isset($block->options) ? unserialize($block->options) : array();
$groups = array(
BLOCK_ATTRIBUTES_BLOCK,
BLOCK_ATTRIBUTES_TITLE,
BLOCK_ATTRIBUTES_CONTENT,
);
foreach ($attributes as $attribute => $info) {
// If no scope is set, this attribute should be available to all scopes.
if (!isset($info['scope'])) {
$info['scope'] = $groups;
}
// Define fields for each scope.
foreach ($info['scope'] as $group) {
// Merge in the proper default value.
if (isset($block_options[$group][$attribute])) {
// If the block already has this attribute, use it.
$info['form']['#default_value'] = $block_options[$group][$attribute];
}
elseif ($form['#form_id'] == 'block_admin_configure') {
// If no value was provided, use the raw default (usually empty).
$info['form']['#default_value'] = $info['default'];
}
$form['settings']['options'][$group][$attribute] = $info['form'] + array(
'#access' => $info['enabled'],
);
}
}
// Restrict access to the new form elements.
$user_has_access = user_access('administer block attributes');
foreach ($groups as $group) {
// Check whether the fieldsets contain any elements. If not, hide them.
$has_visible_children = (bool) element_get_visible_children($form['settings']['options'][$group]);
$form['settings']['options'][$group]['#access'] = $has_visible_children && $user_has_access;
}
// Add a submit callback to save submitted attributes values.
$form['#submit'][] = 'block_attributes_form_submit';
}