You are here

block_aria_landmark_roles.module in Block ARIA Landmark Roles 6

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.
 */

/**
 * Implementation of 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(
      'banner',
      'navigation',
      'search',
      'main',
      'complementary',
      'contentinfo',
    ));

    // Create the additional form elements.
    $form['block_aria_role'] = array(
      '#title' => t('Block ARIA Landmark Role settings'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#weight' => 0,
    );
    $form['block_aria_role']['role'] = array(
      '#title' => t('ARIA Landmark Role'),
      '#description' => t('Specify an ARIA landmark role to add to this block.'),
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => _block_aria_landmark_roles_get_role($block),
    );

    // Add extra submission function.
    $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 (isset($form_state['values']['role']) && user_access('administer blocks')) {
    $module = $form_state['values']['module'];
    $delta = $form_state['values']['delta'];
    $role = $form_state['values']['role'];

    // Delete any existing role.
    db_query("DELETE FROM {block_aria_landmark_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);

    // Save the new role.
    if (!empty($form_state['values']['role'])) {
      $record = new stdClass();
      $record->module = $module;
      $record->delta = $delta;
      $record->role = $role;
      drupal_write_record('block_aria_landmark_roles', $record);
    }
  }
}

/**
 * Find an ARIA landmark role for a certain block.
 *
 * @param obj $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) {
  $role = db_result(db_query("SELECT role FROM {block_aria_landmark_roles} WHERE module = '%s' AND delta = '%s'", $block->module, $block->delta));
  return $role ? $role : '';
}

/**
 * Implementation of hook_preprocess_HOOK().
 */
function block_aria_landmark_roles_preprocess_block(&$variables) {
  $block = (object) array(
    'module' => $variables['block']->module,
    'delta' => $variables['block']->delta,
  );
  $role = _block_aria_landmark_roles_get_role($block);
  $variables['aria_role'] = $role ? 'role="' . $role . '"' : NULL;
}

Functions

Namesort descending Description
block_aria_landmark_roles_form_alter Implementation of hook_form_alter().
block_aria_landmark_roles_form_submit Form submission handler for the ARIA landmark role.
block_aria_landmark_roles_preprocess_block Implementation of hook_preprocess_HOOK().
_block_aria_landmark_roles_get_role Find an ARIA landmark role for a certain block.