diff --git a/tableofcontents.module b/tableofcontents.module index 2c73cbf..dcd2f5b 100644 --- a/tableofcontents.module +++ b/tableofcontents.module @@ -86,49 +86,52 @@ function tableofcontents_filter_settings_submit($form, &$form_state) { * Implementation of hook_field_attach_view_alter(&$output, $context) */ function tableofcontents_field_attach_view_alter(&$output, $context) { - // Find out if we're processing this 'body' - if (isset($output['body'])) { - if ($context['view_mode']=='full') { - $entity_type = $context['entity_type']; - list($id, $vid, $bundle) = entity_extract_ids($entity_type, $context['entity']); - $text = $original = $output['body'][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, 'body', $text))) { - // We haven't. So produce the [toc] fully rendered - $html = theme('tableofcontents_toc', array('toc' => $toc)); - tableofcontents_cache_set($entity_type, $bundle, $vid, 'body', $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['body'][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, $html, $text); - } + 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'); + // 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'); } - drupal_add_js($path . '/js/tableofcontents.js'); - drupal_add_css($path . '/tableofcontents.css'); } - } - // Remove any leftover [toc] - $output['body'][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $output['body'][0]['#markup']); - if (strpos($output['body'][0]['#markup'], '[toc')!==FALSE) { - $output['body'][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $output['body'][0]['#markup'] . ']'); + // 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'] . ']'); + } } } }