You are here

function block_attributes_preprocess_block in Block Attributes 7

Same name and namespace in other branches
  1. 8 block_attributes.module \block_attributes_preprocess_block()

Implements MODULE_preprocess_HOOK().

Extend block's attributes with any user defined HTML attributes.

See also

theme_preprocess_block()

File

./block_attributes.module, line 66
Enhanced control over the HTML attributes of any Block.

Code

function block_attributes_preprocess_block(&$vars) {
  if (isset($vars['block']->options)) {

    // Unserialize and load an array containing all of block's attributes.
    if (is_string($vars['block']->options)) {

      // Modify the variable so that the data stays an array.
      $vars['block']->options = unserialize($vars['block']->options);
    }
    $block_options = $vars['block']->options;

    // Process block level attributes: scope BLOCK_ATTRIBUTES_BLOCK.
    if (!empty($block_options[BLOCK_ATTRIBUTES_BLOCK])) {

      // Specific handling for several template variables as HTML attributes.
      // Block's HTML ID: block_html_id.
      if (isset($block_options[BLOCK_ATTRIBUTES_BLOCK]['id'])) {

        // Overwrite default generated ID with user defined value.
        $vars['block_html_id'] = $block_options[BLOCK_ATTRIBUTES_BLOCK]['id'];
        unset($block_options[BLOCK_ATTRIBUTES_BLOCK]['id']);
      }

      // Block's HTML classes: classes_array.
      if (isset($block_options[BLOCK_ATTRIBUTES_BLOCK]['class'])) {

        // Merge user defined classes with existing ones.
        $vars['classes_array'] = array_merge($vars['classes_array'], $block_options[BLOCK_ATTRIBUTES_BLOCK]['class']);
        unset($block_options[BLOCK_ATTRIBUTES_BLOCK]['class']);
      }

      // All other block HTML attributes, such as accesskey: attributes_array.
      // Remaining values for the block scope should all be global attributes.
      if (isset($block_options[BLOCK_ATTRIBUTES_BLOCK])) {

        // Merge user defined attributes with existing ones.
        $vars['attributes_array'] = array_merge($vars['attributes_array'], $block_options[BLOCK_ATTRIBUTES_BLOCK]);
      }
    }

    // Process Block Title attributes: scope BLOCK_ATTRIBUTES_TITLE.
    // All Block Title HTML attributes, such as class: title_attributes_array.
    if (!empty($block_options[BLOCK_ATTRIBUTES_TITLE])) {

      // Merge user defined attributes with existing ones.
      $vars['title_attributes_array'] = array_merge($vars['title_attributes_array'], $block_options[BLOCK_ATTRIBUTES_TITLE]);
    }

    // Process Block Content attributes: scope BLOCK_ATTRIBUTES_CONTENT.
    // All Block content HTML attributes, such as ID: content_attributes_array.
    if (!empty($block_options[BLOCK_ATTRIBUTES_CONTENT])) {

      // Merge user defined attributes with existing ones.
      $vars['content_attributes_array'] = array_merge($vars['content_attributes_array'], $block_options[BLOCK_ATTRIBUTES_CONTENT]);
    }
  }
}