diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index dc2847d..40efe99 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -174,6 +174,11 @@ function taxonomy_field_extra_fields() { 'description' => t('Term description'), 'weight' => 0, ), + 'taxonomy_term_nodes' => array( + 'label' => t('Tagged nodes'), + 'description' => t('All nodes that have been tagged with term'), + 'weight' => 5, + ), ), ); } @@ -687,6 +692,127 @@ function taxonomy_term_delete($tid) { } /** + * Generate an array which displays a term detail page. + * + * @param term + * A taxonomy term object. + * @return + * A $page element suitable for use by drupal_page_render(). + */ +function taxonomy_term_show($term) { + return taxonomy_term_view_multiple(array($term->tid => $term), 'full'); +} + +/** + * Construct a drupal_render() style array from an array of loaded terms. + * + * @param $terms + * An array of taxonomy terms as returned by taxonomy_term_load_multiple(). + * @param $view_mode + * View mode, e.g. 'full', 'teaser'... + * @param $weight + * An integer representing the weight of the first node in the list. + * @param $langcode + * (optional) A language code to use for rendering. Defaults to the global + * content language of the current request. + * + * @return + * An array in the format expected by drupal_render(). + */ +function taxonomy_term_view_multiple($terms, $view_mode = 'teaser', $weight = 0, $langcode = NULL) { + field_attach_prepare_view('taxonomy_term', $terms, $view_mode, $langcode); + entity_prepare_view('taxonomy_term', $terms, $langcode); + $build = array(); + foreach ($terms as $term) { + $build['taxonomy_terms'][$term->tid] = taxonomy_term_view($term, $view_mode, $langcode); + $build['taxonomy_terms'][$term->tid]['#weight'] = $weight; + $weight++; + } + $build['taxonomy_terms']['#sorted'] = TRUE; + return $build; +} + +/** + * Builds a structured array representing the term's content. + * + * The content built for the taxonomy term (field values, file attachments or + * other term components) will vary depending on the $view_mode parameter. + * + * Drupal core defines the following view modes for terms, with the following + * default use cases: + * - full (default): term is being displayed on its own page (taxonomy/term/123) + * Contributed modules might define additional view modes, or use existing + * view modes in additional contexts. + * + * @param $term + * A taxonomy term object. + * @param $view_mode + * View mode, e.g. 'full', 'teaser'... + * @param $langcode + * (optional) A language code to use for rendering. Defaults to the global + * content language of the current request. + */ +function taxonomy_term_build_content($term, $view_mode = 'full', $langcode = NULL) { + if (!isset($langcode)) { + $langcode = $GLOBALS['language_content']->language; + } + + // Remove previously built content, if exists. + $term->content = array(); + + // Try to add in the core taxonomy pieces like description and nodes + $type = 'taxonomy_term'; + $entity_data = entity_get_info($type); + $entity_ids = entity_extract_ids($type, $term); + $settings = field_view_mode_settings($type, $entity_ids[2]); + $fields = field_extra_fields_get_display($type, $entity_ids[2], $view_mode); + if (!empty($term->description) && isset($fields['description']) && $fields['description']['visible']) { + $term->content['description'] = array( + '#markup' => check_markup($term->description, $term->format, '', TRUE), + '#weight' => $fields['description']['weight'], + '#prefix' => '
', + '#suffix' => '
', + ); + } + + if (isset($fields['taxonomy_term_nodes']) && $fields['taxonomy_term_nodes']['visible']) { + if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) { + $nodes = node_load_multiple($nids); + // I'm not sure on how to tackle this given the pager/whatnot. It will be in main build. + $node_build = node_view_multiple($nodes); + $term->content['term_nodes'] = array( + '#markup' => drupal_render($node_build), + '#weight' => $fields['taxonomy_term_nodes']['weight'], + ); + $term->content['term_nodes_pager'] = array( + '#theme' => 'pager', + '#weight' => $fields['taxonomy_term_nodes']['weight'] + 1, + ); + } + else { + $term->content['no_content'] = array( + '#prefix' => '

', + '#markup' => t('There is currently no content classified with this term.'), + '#suffix' => '

', + '#weight' => $fields['taxonomy_term_nodes']['weight'], + ); + } + } + + // Build fields content. + // In case of a multiple view, taxonomy_term_view_multiple() already ran the + // 'prepare_view' step. An internal flag prevents the operation from running + // twice. + field_attach_prepare_view('taxonomy_term', array($term->tid => $term), $view_mode, $langcode); + entity_prepare_view('taxonomy_term', array($term->tid => $term), $langcode); + $term->content += field_attach_view('taxonomy_term', $term, $view_mode, $langcode); + + // Allow modules to make their own additions to the taxonomy term. + module_invoke_all('taxonomy_term_view', $term, $view_mode, $langcode); + module_invoke_all('entity_view', $term, 'taxonomy_term', $view_mode, $langcode); +} + +/** * Generate an array for rendering the given term. * * @param $term @@ -705,34 +831,26 @@ function taxonomy_term_view($term, $view_mode = 'full', $langcode = NULL) { $langcode = $GLOBALS['language_content']->language; } - field_attach_prepare_view('taxonomy_term', array($term->tid => $term), $view_mode, $langcode); - entity_prepare_view('taxonomy_term', array($term->tid => $term), $langcode); + // Populate $node->content with a render() array. + taxonomy_term_build_content($term, $view_mode, $langcode); + $build = $term->content; + + // We don't need duplicate rendering info in node->content. + unset($term->content); - $build = array( + $build += array( '#theme' => 'taxonomy_term', '#term' => $term, '#view_mode' => $view_mode, '#language' => $langcode, ); - $build += field_attach_view('taxonomy_term', $term, $view_mode, $langcode); - - // Add term description if the term has one. - if (!empty($term->description)) { - $build['description'] = array( - '#markup' => check_markup($term->description, $term->format, '', TRUE), - '#weight' => 0, - '#prefix' => '
', - '#suffix' => '
', - ); - } - $build['#attached']['css'][] = drupal_get_path('module', 'taxonomy') . '/taxonomy.css'; - // Allow modules to modify the structured term. + // Allow modules to modify the structured taxonomy term. $type = 'taxonomy_term'; drupal_alter(array('taxonomy_term_view', 'entity_view'), $build, $type); - + return $build; } diff --git a/modules/taxonomy/taxonomy.pages.inc b/modules/taxonomy/taxonomy.pages.inc index 0cca252..675057a 100644 --- a/modules/taxonomy/taxonomy.pages.inc +++ b/modules/taxonomy/taxonomy.pages.inc @@ -14,6 +14,11 @@ * The page content. */ function taxonomy_term_page($term) { + // If there is a menu link to this term, the link becomes the last part + // of the active trail, and the link name becomes the page title. + // Thus, we must explicitly set the page title to be the node title. + $uri = entity_uri('taxonomy_term', $term); + // Build breadcrumb based on the hierarchy of the term. $current = (object) array( 'tid' => $term->tid, @@ -30,30 +35,11 @@ function taxonomy_term_page($term) { drupal_set_breadcrumb($breadcrumb); drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name); - $build = array(); - - $build['term_heading'] = array( - '#prefix' => '
', - '#suffix' => '
', - 'term' => taxonomy_term_view($term, 'full'), - ); - - if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) { - $nodes = node_load_multiple($nids); - $build += node_view_multiple($nodes); - $build['pager'] = array( - '#theme' => 'pager', - '#weight' => 5, - ); - } - else { - $build['no_content'] = array( - '#prefix' => '

', - '#markup' => t('There is currently no content classified with this term.'), - '#suffix' => '

', - ); - } - return $build; + // Set the term path as the canonical URL to prevent duplicate content. + drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE); + // Set the non-aliased path as a default shortlink. + drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE); + return taxonomy_term_show($term); } /**