You are here

block_aria_landmark_roles.module in Block ARIA Landmark Roles 7.2

Adds additional elements on block administration forms to add ARIA landmark roles.

File

block_aria_landmark_roles.module
View source
<?php

/**
 * @file
 * Adds additional elements on block administration forms to add ARIA landmark roles.
 */

/**
 * Implements hook_form_alter().
 *
 * Adds additional elements to the 'add block' and 'configure block' forms.
 */
function block_aria_landmark_roles_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'block_add_block_form' || $form_id == 'block_admin_configure') {

    // Build the block object.
    $block = new stdClass();
    $block->module = $form['module']['#value'];
    $block->delta = $form['delta']['#value'];
    $options[] = t('- None -');
    $options += drupal_map_assoc(array(
      'application',
      'banner',
      'complementary',
      'contentinfo',
      'form',
      'main',
      'navigation',
      'search',
    ));

    // Create the additional form elements.
    $form['block_aria_role'] = array(
      '#title' => t('Block ARIA Landmark Roles settings'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
    );
    $form['block_aria_role']['role'] = array(
      '#title' => t('Landmark Role'),
      '#description' => t('Add an ARIA landmark role to this block.'),
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => _block_aria_landmark_roles_get_role($block),
    );
    $form['block_aria_role']['aria_label'] = array(
      '#title' => t('Label'),
      '#description' => t('Add an ARIA label to this block.'),
      '#type' => 'textfield',
      '#default_value' => _block_aria_landmark_roles_get_label($block),
    );
    $form['#submit'][] = 'block_aria_landmark_roles_form_submit';
  }
}

/**
 * Form submission handler for the ARIA landmark role.
 *
 * Saves the data to the block_aria_landmark_roles table.
 */
function block_aria_landmark_roles_form_submit($form, &$form_state) {
  if (user_access('administer blocks')) {
    $module = $form_state['values']['module'];
    $delta = $form_state['values']['delta'];

    // ARIA Labels.
    if (isset($form_state['values']['aria_label'])) {
      $label = $form_state['values']['aria_label'];
      variable_set("block_aria_landmark_roles_label__{$module}__{$delta}", $label);
    }

    // ARIA Landmark roles.
    if (isset($form_state['values']['role'])) {
      $role = $form_state['values']['role'];
      variable_set("block_aria_landmark_roles__{$module}__{$delta}", $role);
    }
  }
}

/**
 * Find an ARIA landmark role for a certain block.
 *
 * @param \stdClass $block
 *  An object containing the name of the module and the delta of the block.
 *
 * @return string|bool
 *  Returns the role if one was found. If not, returns nothing.
 */
function _block_aria_landmark_roles_get_role($block) {
  return variable_get("block_aria_landmark_roles__{$block->module}__{$block->delta}");
}

/**
 * Find an ARIA landmark label for a certain block.
 *
 * @param \stdClass $block
 *   An object containing the name of the module and the delta of the block.
 *
 * @return string|bool
 *   Returns the label if one was found. If not, returns nothing.
 */
function _block_aria_landmark_roles_get_label($block) {
  return variable_get("block_aria_landmark_roles_label__{$block->module}__{$block->delta}");
}

/**
 * Implements hook_preprocess_block().
 *
 * Adds additional variables for ARIA landmark roles.
 */
function block_aria_landmark_roles_preprocess_block(&$variables) {
  $block = $variables['block'];
  $role = _block_aria_landmark_roles_get_role($block);
  $label = _block_aria_landmark_roles_get_label($block);
  if (!empty($role)) {

    // Add the role into the attributes array.
    $variables['attributes_array']['role'] = $role;
  }
  if (!empty($label)) {

    // Add the label into the attributes array.
    $variables['attributes_array']['aria-label'] = $label;
  }
}

Functions

Namesort descending Description
block_aria_landmark_roles_form_alter Implements hook_form_alter().
block_aria_landmark_roles_form_submit Form submission handler for the ARIA landmark role.
block_aria_landmark_roles_preprocess_block Implements hook_preprocess_block().
_block_aria_landmark_roles_get_label Find an ARIA landmark label for a certain block.
_block_aria_landmark_roles_get_role Find an ARIA landmark role for a certain block.