You are here

function tableofcontents_field_attach_view_alter in Table of Contents 7.2

Implementation of hook_field_attach_view_alter(&$output, $context)

File

./tableofcontents.module, line 88
This is a filter module to generate a collapsible jquery enabled mediawiki style table of contents based on <h[1-6]> tags. Transforms header tags into named anchors.

Code

function tableofcontents_field_attach_view_alter(&$output, $context) {
  foreach ($output as $value) {

    // Find out if we're processing this 'field' : text_with_summary for  body , text_long for comments
    if (isset($value['#field_type']) && strpos($value['#field_type'], 'text') !== FALSE) {
      $field_name = $value['#field_name'];
      if ($context['view_mode'] == 'full') {
        $entity_type = $context['entity_type'];
        list($id, $vid, $bundle) = entity_extract_ids($entity_type, $context['entity']);
        $text = $original = $output[$field_name][0]['#markup'];

        // Apply some tests to see if we're putting a [toc] on this page
        if (_tableofcontents_apply_toc($text, $entity_type, $bundle)) {

          // Process the headers on this page (we have to do this)
          module_load_include('inc', 'tableofcontents');
          $toc =& tableofcontents_toc();
          $text = _tableofcontents_headers($text);

          // Check to see if we have a cached copy of the [toc]
          if (!($html = tableofcontents_cache_get($entity_type, $bundle, $vid, $field_name, $text))) {

            // We haven't. So produce the [toc] fully rendered
            $html = theme('tableofcontents_toc', array(
              'toc' => $toc,
            ));
            tableofcontents_cache_set($entity_type, $bundle, $vid, $field_name, $text, $html);
          }

          // Insert the rendered [toc] in the right place.
          if ($toc['on_off']['automatic'] != 3) {

            // Automatic "3" means don't put it on the page (it may go into a block).
            $output[$field_name][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, $html, $text);
          }

          // Add the styling and controls
          $settings = array(
            'tableofcontents' => array(
              'collapse' => !!$toc['box']['collapsed'],
              'scroll' => !!$toc['back_to_top']['scroll'],
            ),
          );
          drupal_add_js($settings, 'setting');
          $path = drupal_get_path('module', 'tableofcontents');
          if (!empty($toc['back_to_top']['scroll'])) {
            drupal_add_js($path . '/js/jquery.scrollTo-min.js');
            drupal_add_js($path . '/js/jquery.localscroll-min.js');
          }
          drupal_add_js($path . '/js/tableofcontents.js');
          drupal_add_css($path . '/tableofcontents.css');
        }
      }

      // Remove any leftover [toc]
      $output[$field_name][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $output[$field_name][0]['#markup']);
      if (strpos($output[$field_name][0]['#markup'], '[toc') !== FALSE) {
        $output[$field_name][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $output[$field_name][0]['#markup'] . ']');
      }
    }
  }
}