taxonomy_display.module 9.54 KB
Newer Older
Cody Craven's avatar
Cody Craven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
// $Id$

/**
 * @file
 * Hooks for the taxonomy display module.
 */

/**
 * Return just the term display sub-form.
 *
 * @return renderable array
 */
function taxonomy_display_ajax_term_display_callback($form, $form_state) {
  return $form['additional_settings']['taxonomy_display']['term_display_form'];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  // Only proceed if we are on taxonomy_term entity type and the user has admin
  // taxonomy display.
  if ($form['#entity_type'] != 'taxonomy_term' || !user_access('administer taxonomy display')) {
    return;
  }

  // Make things easy on users getting started with the module.
  // We require the 'full' #view_mode to configure the taxonomy term page.
  if ($form['#view_mode'] == 'default') {
    $field = array(
      '#title' => 'Term page display',
      '#type' => 'fieldset',
    );
    // TODO: There's got to be a better way to find out if 'full' is enabled,
    // need to look.
    foreach ($form['additional_settings']['modes']['view_modes_custom']['#default_value'] as $value) {
      if ($value == 'full') {
        $field['#description'] = 'To alter the term page display you need to go to the <em>Taxonomy term page</em> view mode.';
        break;
      }
    }
    if (!isset($field['#description'])) {
      $field['#description'] = 'Enable the <em>Taxonomy term page</em> view mode under the <em>Custom display settings</em> tab to alter the term page display.';
    }
    $form['additional_settings']['taxonomy_display'] = $field;
  }

  // Only proceed if we are on the full display mode.
  if ($form['#view_mode'] != 'full') {
    return;
  }

  $stored_settings = db_select('taxonomy_display', 'td')
    ->fields('td')
    ->condition('machine_name', $form['#bundle'])
    ->execute()
    ->fetchAssoc();

  $form['additional_settings']['taxonomy_display'] = array(
    '#description' => 'Configure how the taxonomy term display page should be presented when viewed for this vocabulary.',
    '#title' => 'Term page display',
    '#type' => 'fieldset',
  );
  $fieldset =& $form['additional_settings']['taxonomy_display'];
  if (isset($form_state['values']['additional_settings']['taxonomy_display'])) {
    $state_values =& $form_state['values']['additional_settings']['taxonomy_display'];
  }


  // Populate $options with plugins that are available.
  $options = taxonomy_display_plugins('term');
  // TODO: Rewrite supporting missing plugins.
  // Discover which option should be selected from term display plugins.
  if (isset($state_values) && isset($state_values['term_display_select']) &&
      array_key_exists($state_values['term_display_select'], $options)) {
    $selected = $state_values['term_display_select'];
  }
  elseif (isset($stored_settings['term_display_plugin']) &&
      array_key_exists($stored_settings['term_display_plugin'], $options)) {
    $selected = $stored_settings['term_display_plugin'];
  }
  else {
    $selected = 'TaxonomyDisplayTermDisplayHandlerCore';
  }
  $fieldset['term_display_select'] = array(
    '#ajax' => array(
      'callback' => 'taxonomy_display_ajax_term_display_callback',
      'wrapper' => 'term_display_form_replace',
    ),
    '#default_value' => $selected,
    '#description' => t('Select which plugin you would like to display the term\'s content.'),
    '#options' => $options,
    '#title' => t('Term display'),
    '#type' => 'select',
  );

  if (isset($form_state['storage']['term_display_last_handler'])) {
    $last_handler = $form_state['storage']['term_display_last_handler'];
  }
  $form_state['storage']['term_display_last_handler'] = $selected;

  $fieldset['term_display_form'] = array(
    '#prefix' => '<div id="term_display_form_replace">',
    '#suffix' => '</div>',
    '#title' => t('!label term display options', array('!label' => $options[$selected])),
    '#tree' => TRUE,
    '#type' => 'fieldset',
  );
  $term_display_class = new $selected;
  // Get the fieldset from our handler, if the handler was the one used on the
  // last form build then send the array of values for it's field.
  if ($last_handler == $selected) {
    $term_display_class->formFieldset($fieldset['term_display_form'], $state_values['term_display_form']);
  }
  else {
    $term_display_class->formFieldset($fieldset['term_display_form'], array());
  }
}

/**
 * Implements of hook_menu_alter().
 */
function taxonomy_display_menu_alter(&$items) {
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'taxonomy_display_taxonomy_term_page';
  $items['taxonomy/term/%taxonomy_term']['title callback'] = 'taxonomy_display_taxonomy_term_title';
  $items['taxonomy/term/%taxonomy_term']['file'] = 'taxonomy_display.module';
  $items['taxonomy/term/%taxonomy_term']['module'] = 'taxonomy_display';
}

/**
 * Implements hook_permission().
 */
function taxonomy_display_permission() {
  $permissions = array(
    'administer taxonomy display' => array(
      'title' => t('Administer taxonomy display'),
      'description' => t('Change the display settings for taxonomy term pages in each vocabulary.'),
    ),
  );
  return $permissions;
}

/**
 * Retrieve an array of Taxonomy Display plugins.
 * 
 * @param null|string $type
 *   Only retrieve plugins of a specific type, expected values 'term' and 
 *   'associated'. By default, NULL, this will return an array with both.
 * 
 * @return array|void
 *   If an expected value is provided for $type, or no value is provided, an
 *   array will be returned. If an unexpected value is provided nothing will be
 *   returned.
 */
function taxonomy_display_plugins($type = NULL) {
  // TODO: Add caching, likely not a big issue as this is only called in admin
  // areas, but still.

  $plugins = module_invoke_all('taxonomy_display_plugins');

  // Expose our retrieved plugins to altering.
  drupal_alter('taxonomy_display_plugins', $plugins);

  if (is_null($type)) {
    return $plugins;
  }

  if (array_key_exists($type, $plugins)) {
    return $plugins[$type];
  }
}

/**
 * Implements hook_taxonomy_display_plugins().
 *
 * Taxonomy Display invokes this hook when looking for plugins for displaying
 * taxonomy term and taxonomy term's associated content.
 */
function taxonomy_display_taxonomy_display_plugins() {
  // To add custom plugins in your own hook implementation return an array with
  // the format below:
  return array(
    // In the two arrays 'associated' and 'term', provide the implementing class
    // name of your handler as the keys and the text to be displayed to the user
    // for display as the value.
    // Note: All of the class's files are in our module's .info files array.
    'associated' => array(
      // TODO: Add core, hidden, views
    ),
    'term' => array(
      'TaxonomyDisplayTermDisplayHandlerCore' => t('Core'),
      'TaxonomyDisplayTermDisplayHandlerHidden' => t('Hidden'),
    ),
  );
}

/**
 * Page callback; displays all nodes associated with a term.
 *
 * @param $term
 *   A term object.
 *
 * @return
 *   The page content.
 */
function taxonomy_display_taxonomy_term_page($term) {
  // TODO: Select record from the database and run through plugins.

  module_load_include('inc', 'taxonomy', 'taxonomy.pages');
  $build = taxonomy_term_page($term);

  return $build;
}

/**
 * Title callback; display title for term pages.
 *
 * @param $term
 *   A term object.
 *
 * @return
 *   The term name to be used as the page title.
 */
function taxonomy_display_taxonomy_term_title($term) {
  return taxonomy_term_title($term);
}

/**
 * Implements taxonomy_vocabulary_delete().
 *
 * @todo test this works
 */
function taxonomy_display_taxonomy_vocabulary_delete($vocabulary) {
  // We use this function to delete taxonomy displays other than as a hook
  // invocation, one such case works off taxonomy_vocabulary_get_names() which
  // provides the vocabularies as arrays.
  if (is_array($vocabulary)) {
    $vocabulary = (object) $vocabulary;
  }

  // Delete our display record for the term being removed.
  $count = (bool) db_delete('taxonomy_display')
    ->condition('machine_name', $vocabulary->machine_name)
    ->execute();

  // If a record was deleted then log it in watchdog.
  if ($count) {
    watchdog('taxonomy_display', 'Taxonomy display settings deleted for %name in response to the vocabulary being deleted.', array('%name' => $vocabulary->machine_name), WATCHDOG_NOTICE);
  }
}

/**
 * Implements taxonomy_vocabulary_insert().
 */
function taxonomy_display_taxonomy_vocabulary_insert($vocabulary) {
  // Upon inserting a vocabulary term add it to our displays with the default
  // selection.
  // TODO: Insert table
}

/**
 * Implements taxonomy_vocabulary_update().
 *
 * @todo test this works
 */
function taxonomy_display_taxonomy_vocabulary_update($vocabulary) {
  // If the machine name is changed update our display table so that the display
  // settings still apply.
  if ($vocabulary->old_machine_name != $vocabulary->machine_name) {
    try {
      $count = (bool) db_update('taxonomy_display')
        ->fields(array(
          'machine_name' => $vocabulary->machine_name,
        ))
        ->condition('machine_name', $vocabulary->old_machine_name)
        ->execute();

      if ($count) {
        watchdog('taxonomy_display', 'Taxonomy display vocabulary changed machine name from %old to %new in response to the vocabulary machine name being altered.', array('%old' => $vocabulary->old_machine_name, '%new' => $vocabulary->machine_name), WATCHDOG_NOTICE);
      }
    }
    catch(Exception $e) {
      drupal_set_message(t('db_update failed. Message = %message, query= %query',
        array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
    }
  }
}