You are here

tableofcontents_block.module in Table of Contents 7

Same filename and directory in other branches
  1. 6.3 tableofcontents_block/tableofcontents_block.module

main file for module providing a block

File

tableofcontents_block/tableofcontents_block.module
View source
<?php

/**
 * @file
 * main file for module providing a block
 */

/**
 * Create the block if user has permission and we are viewing
 * a node and the node has enough data to generate a table of
 * contents.
 */
function _tableofcontents_block_view() {
  global $_tableofcontents_block_processing;
  global $_tableofcontents_block_toc;

  // check whether we are viewing a node
  if (arg(0) != 'node' || !is_numeric(arg(1))) {

    // no block
    // no block
    return;
  }
  $node = node_load(arg(1));

  // verify user's access to view the node
  if (!node_access('view', $node)) {
    return;
  }
  $_tableofcontents_block_processing = TRUE;
  $text = node_view($node, 'full', $node->language);
  unset($_tableofcontents_block_processing);
  module_load_include('pages.inc', 'tableofcontents');
  $_tableofcontents_block_toc = '[toc]';
  $text = _tableofcontents_process(drupal_render($text['body']), 'block');
  unset($_tableofcontents_block_toc);
  if ($text) {
    return $text;
  }

  // no block
}

/**
 * Create a form to setup the Table of contents block.
 */
function _tableofcontents_block_form() {

  // we get the filter form
  module_load_include('admin.inc', 'tableofcontents');
  $filter_form = _tableofcontents_settings('block');

  // then we change a few things
  $form = $filter_form['tableofcontents'];

  // adapt some messages
  $form['tableofcontents_min_limit_block']['#title'] = t('Number of headers to show table of contents');
  $form['tableofcontents_min_limit_block']['#description'] = t('The minimum number of headers before the table of contents is shown.');

  // forced values are never shown to the user
  unset($form['tableofcontents_automatic_block']);

  // there is no top or bottom! so we always use top
  unset($form['tableofcontents_hide_table_block']);
  unset($form['tableofcontents_allow_override_block']);
  unset($form['tableofcontents_back_to_top_block']);
  unset($form['tableofcontents_back_to_top_location_block']);
  unset($form['tableofcontents_back_to_top_anchor_block']);
  unset($form['tableofcontents_scroll_back_to_top_block']);
  unset($form['tableofcontents_number_headers_block']);
  unset($form['tableofcontents_remove_teaser_block']);
  return $form;
}
function _tableofcontents_block_save($edit) {
  foreach ($edit as $key => $value) {
    if (substr($key, 0, 15) == 'tableofcontents') {
      variable_set($key, $value);
    }
  }
}

/**
 * Implements hook_block_info().
 */
function tableofcontents_block_block_info() {
  $block['tableofcontents_block'] = array(
    'info' => t('Table of contents'),
  );
  return $block;
}

/**
 * Implements hook_block_view().
 */
function tableofcontents_block_block_view($delta) {
  $block = array();
  switch ($delta) {
    case 'tableofcontents_block':
      $block['subject'] = t('Table of contents');
      $block['content'] = _tableofcontents_block_view();
      break;
  }
  return $block;
}

/**
 * Implements hook_block_configure().
 */
function tableofcontents_block_block_configure($delta) {
  if ($delta == 'tableofcontents_block') {
    $form = _tableofcontents_block_form();
    return $form;
  }
}

/**
 * Implements hook_block_save().
 */
function tableofcontents_block_block_save($delta = '', $edit = array()) {
  if ($delta == 'tableofcontents_block') {
    _tableofcontents_block_save($edit);
  }
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function tableofcontents_block_form_filter_admin_format_form_alter(&$form, &$form_state, $form_id) {

  // verify that the table of contents is turned in that filter.
  if (isset($form['filters']['settings']['filter_toc']['tableofcontents'])) {
    $format = $form['#format']->format;

    // add block specific settings to this filter
    $form['filters']['settings']['filter_toc']['tableofcontents']['tableofcontents_block'] = array(
      '#type' => 'fieldset',
      '#title' => t('Table of Contents block'),
      '#collapsible' => TRUE,
    );
    $form['filters']['settings']['filter_toc']['tableofcontents']['tableofcontents_block']['tableofcontents_block_only_table_' . $format] = array(
      '#title' => t('Block-only table of contents'),
      '#type' => 'checkbox',
      '#default_value' => variable_get('tableofcontents_block_only_table_' . $format, FALSE),
      '#description' => 'When checked, all the table of contents are removed from node bodies, only headers are altered. This allows for a block-only table of contents.',
    );
  }
}

Functions

Namesort descending Description
tableofcontents_block_block_configure Implements hook_block_configure().
tableofcontents_block_block_info Implements hook_block_info().
tableofcontents_block_block_save Implements hook_block_save().
tableofcontents_block_block_view Implements hook_block_view().
tableofcontents_block_form_filter_admin_format_form_alter Implementation of hook_form_FORM_ID_alter().
_tableofcontents_block_form Create a form to setup the Table of contents block.
_tableofcontents_block_save
_tableofcontents_block_view Create the block if user has permission and we are viewing a node and the node has enough data to generate a table of contents.