Projet

Général

Profil

Paste
Télécharger (6,55 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / taxonomy / taxonomy.pages.inc @ 76597ebf

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Page callbacks for the taxonomy module.
6
 */
7
8
/**
9
 * Menu callback; displays all nodes associated with a term.
10
 *
11
 * @param $term
12
 *   The taxonomy term.
13
 * @return
14
 *   The page content.
15
 */
16
function taxonomy_term_page($term) {
17
  // If there is a menu link to this term, the link becomes the last part of
18
  // the active trail, and the link name becomes the page title. Thus, we must
19
  // explicitly set the page title to be the term title.
20
  drupal_set_title($term->name);
21
22
  // Build breadcrumb based on the hierarchy of the term.
23
  $current = (object) array(
24
    'tid' => $term->tid,
25
  );
26
  // @todo This overrides any other possible breadcrumb and is a pure hard-coded
27
  //   presumption. Make this behavior configurable per vocabulary or term.
28
  $breadcrumb = array();
29
  while ($parents = taxonomy_get_parents($current->tid)) {
30
    $current = array_shift($parents);
31
    $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
32
  }
33
  $breadcrumb[] = l(t('Home'), NULL);
34
  $breadcrumb = array_reverse($breadcrumb);
35
  drupal_set_breadcrumb($breadcrumb);
36
  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);
37
38
  // Set the term path as the canonical URL to prevent duplicate content.
39
  $uri = entity_uri('taxonomy_term', $term);
40
  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
41
  // Set the non-aliased path as a default shortlink.
42
  drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
43
44
  // Normally we would call taxonomy_term_show() here, but for backwards
45
  // compatibility in Drupal 7 we do not want to do that (it produces different
46
  // data structures and HTML markup than what Drupal 7 released with). Calling
47
  // taxonomy_term_view() directly provides essentially the same thing, but
48
  // allows us to wrap the rendered term in our desired array structure.
49
  $build['term_heading'] = array(
50
    '#prefix' => '<div class="term-listing-heading">',
51
    '#suffix' => '</div>',
52
    'term' => taxonomy_term_view($term, 'full'),
53
  );
54
55
  if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
56
    $nodes = node_load_multiple($nids);
57
    $build += node_view_multiple($nodes);
58
    $build['pager'] = array(
59
      '#theme' => 'pager',
60
      '#weight' => 5,
61
    );
62
  }
63
  else {
64
    $build['no_content'] = array(
65
      '#prefix' => '<p>',
66
      '#markup' => t('There is currently no content classified with this term.'),
67
      '#suffix' => '</p>',
68
    );
69
  }
70
  return $build;
71
}
72
73
/**
74
 * Generate the content feed for a taxonomy term.
75
 *
76
 * @param $term
77
 *   The taxonomy term.
78
 */
79
function taxonomy_term_feed($term) {
80
  $channel['link'] = url('taxonomy/term/' . $term->tid, array('absolute' => TRUE));
81
  $channel['title'] = variable_get('site_name', 'Drupal') . ' - ' . $term->name;
82
  // Only display the description if we have a single term, to avoid clutter and confusion.
83
  // HTML will be removed from feed description.
84
  $channel['description'] = check_markup($term->description, $term->format, '', TRUE);
85
  $nids = taxonomy_select_nodes($term->tid, FALSE, variable_get('feed_default_items', 10));
86
87
  node_feed($nids, $channel);
88
}
89
90
/**
91
 * Page callback: Outputs JSON for taxonomy autocomplete suggestions.
92
 *
93
 * Path: taxonomy/autocomplete
94
 *
95
 * This callback outputs term name suggestions in response to Ajax requests
96
 * made by the taxonomy autocomplete widget for taxonomy term reference
97
 * fields. The output is a JSON object of plain-text term suggestions, keyed by
98
 * the user-entered value with the completed term name appended.  Term names
99
 * containing commas are wrapped in quotes.
100
 *
101
 * For example, suppose the user has entered the string 'red fish, blue' in the
102
 * field, and there are two taxonomy terms, 'blue fish' and 'blue moon'. The
103
 * JSON output would have the following structure:
104
 * @code
105
 *   {
106
 *     "red fish, blue fish": "blue fish",
107
 *     "red fish, blue moon": "blue moon",
108
 *   };
109
 * @endcode
110
 *
111
 * @param $field_name
112
 *   The name of the term reference field.
113
 * @param $tags_typed
114
 *   (optional) A comma-separated list of term names entered in the
115
 *   autocomplete form element. Only the last term is used for autocompletion.
116
 *   Defaults to '' (an empty string).
117
 *
118
 * @see taxonomy_menu()
119
 * @see taxonomy_field_widget_info()
120
 */
121
function taxonomy_autocomplete($field_name = '', $tags_typed = '') {
122
  // If the request has a '/' in the search text, then the menu system will have
123
  // split it into multiple arguments, recover the intended $tags_typed.
124
  $args = func_get_args();
125
  // Shift off the $field_name argument.
126
  array_shift($args);
127
  $tags_typed = implode('/', $args);
128
129
  // Make sure the field exists and is a taxonomy field.
130
  if (!($field = field_info_field($field_name)) || $field['type'] !== 'taxonomy_term_reference') {
131
    // Error string. The JavaScript handler will realize this is not JSON and
132
    // will display it as debugging information.
133
    print t('Taxonomy field @field_name not found.', array('@field_name' => $field_name));
134
    exit;
135
  }
136
137
  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
138
  $tags_typed = drupal_explode_tags($tags_typed);
139
  $tag_last = drupal_strtolower(array_pop($tags_typed));
140
141
  $term_matches = array();
142
  if ($tag_last != '') {
143
144
    // Part of the criteria for the query come from the field's own settings.
145
    $vids = array();
146
    $vocabularies = taxonomy_vocabulary_get_names();
147
    foreach ($field['settings']['allowed_values'] as $tree) {
148
      $vids[] = $vocabularies[$tree['vocabulary']]->vid;
149
    }
150
151
    $query = db_select('taxonomy_term_data', 't');
152
    $query->addTag('translatable');
153
    $query->addTag('term_access');
154
155
    // Do not select already entered terms.
156
    if (!empty($tags_typed)) {
157
      $query->condition('t.name', $tags_typed, 'NOT IN');
158
    }
159
    // Select rows that match by term name.
160
    $tags_return = $query
161
      ->fields('t', array('tid', 'name'))
162
      ->condition('t.vid', $vids)
163
      ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
164
      ->range(0, 10)
165
      ->execute()
166
      ->fetchAllKeyed();
167
168
    $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
169
170
    foreach ($tags_return as $tid => $name) {
171
      $n = $name;
172
      // Term names containing commas or quotes must be wrapped in quotes.
173
      if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
174
        $n = '"' . str_replace('"', '""', $name) . '"';
175
      }
176
      $term_matches[$prefix . $n] = check_plain($name);
177
    }
178
  }
179
180
  drupal_json_output($term_matches);
181
}