taxonomy_display.admin.inc 8.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// $Id$

/**
 * @file
 * Administration form functions for taxonomy display configuration.
 */

/**
 * Helper callback; perform form alterations for taxonomy display admin form.
 *
 * @param array $form
 * @param array $form_state
 *
 * @return void
 *
 * @see taxonomy_display_form_fieldset()
 */
function taxonomy_display_admin_form(&$form, &$form_state) {
20
  // Only proceed if we are on taxonomy_term entity type and the user has admin
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
  // 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;
  }

52
  // Retrieve the stored taxonomy display settings for the vocabulary.
53
54
55
56
57
58
59
60
  $stored_settings = taxonomy_display_fetch_taxonomy_display($form['#bundle']);

  $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'];
61
  $values = NULL;
62
63
64
65
  if (isset($form_state['values']['additional_settings']['taxonomy_display'])) {
    $values =& $form_state['values']['additional_settings']['taxonomy_display'];
  }

66
67
68
  // Build the plugin forms for term and associated content display.
  _taxonomy_display_admin_form_build_plugin_form('term', $fieldset, $values, $stored_settings);
  _taxonomy_display_admin_form_build_plugin_form('associated', $fieldset, $values, $stored_settings);
69
70
71
72
73
74
75
76
77
78
79
80
81

  // Add our #validate and #submit callback which will call plugins' methods.
  $form['#validate'][] = 'taxonomy_display_admin_form_validate';
  $form['#submit'][] = 'taxonomy_display_admin_form_submit';
}

/**
 * Form submit callback; save Taxonomy Display settings for term pages.
 */
function taxonomy_display_admin_form_submit($form, &$form_state) {
  $values =& $form_state['values']['additional_settings']['taxonomy_display'];
  $td_form =& $form['additional_settings']['taxonomy_display'];

82
83
  $save_data = array();

84
  // Perform submit functions for our term display handler.
85
86
87
88
89
90
  if ($values['term_display_select'] != 'missing') {
    $save_data['term_display_plugin'] = $values['term_display_select'];

    $term_display = new $values['term_display_select'];
    $save_data['term_display_options'] = $term_display->formSubmit($td_form['term_display_form'], $values['term_display_form']);
  }
91
92

  // Perform submit functions for our associated content handler.
93
94
95
96
97
98
  if ($values['associated_display_select'] != 'missing') {
    $save_data['associated_display_plugin'] = $values['associated_display_select'];

    $associated_display = new $values['associated_display_select'];
    $save_data['associated_display_options'] = $associated_display->formSubmit($td_form['associated_display_form'], $values['associated_display_form']);
  }
99
100

  // Save data
101
102
103
  if (!empty($save_data)) {
    taxonomy_display_save_taxonomy_display($form['#bundle'], $save_data);
  }
104
105
106
107
108
109
110
111
112
113
}

/**
 * Form validate callback; validate Taxonomy Display settings for term pages.
 */
function taxonomy_display_admin_form_validate($form, &$form_state) {
  $values =& $form_state['values']['additional_settings']['taxonomy_display'];
  $td_form =& $form['additional_settings']['taxonomy_display'];

  // Perform validation functions for our term display handler.
114
115
116
117
  if ($values['term_display_select'] != 'missing') {
    $term_display = new $values['term_display_select'];
    $term_display->formValidate($td_form['term_display_form'], $values['term_display_form']);
  }
118
119

  // Perform validation functions for our associated content display handler.
120
121
122
123
  if ($values['associated_display_select'] != 'missing') {
    $associated_display = new $values['associated_display_select'];
    $associated_display->formValidate($td_form['associated_display_form'], $values['associated_display_form']);
  }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
}

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

/**
 * 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'];
}
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

/**
 * Helper function; build the admin form for plugins.
 * 
 * @param string $type
 *   Either term or associated.
 * @param array $fieldset
 *   Form array that we will modify.
 * @param array|null $values
 *   Array of values submitted to the fieldset, taken from $form_state or null
 *   if the form has not yet been submitted.
 * @param $stored_settings
 *   Associated array from taxonomy_display_fetch_taxonomy_display().
 *
 * @return void
 *
 * @see taxonomy_display_admin_form()
 */
function _taxonomy_display_admin_form_build_plugin_form($type, &$fieldset, &$values, $stored_settings) {
  // Set variables needed based on type
  switch ($type) {
    case 'associated':
      $select_field_title = t('Associated content display');
      $select_field_description = t('Select which plugin you would like to display the term\'s associated content.');
      break;
    case 'term':
      $select_field_title = t('Term display');
      $select_field_description = t('Select which plugin you would like to display the term\'s content.');
      break;
    default:
      return;
  }

  // Populate $options with plugins that are available.
  $options = taxonomy_display_plugins($type);

  // TODO: Finish testing missing plugin support.
  // Add support for missing plugins.
  if (isset($stored_settings[$type . '_plugin_missing'])) {
    $options = array_merge(array('missing' => t('-- Missing plugin --')), $options);
  }

  // Discover which option should be selected from the display plugins.
  if (isset($values[$type . '_display_select']) &&
      $values[$type . '_display_select'] != 'missing' &&
      array_key_exists($values[$type . '_display_select'], $options)) {
    $selected = $values[$type . '_display_select'];
  }
  elseif (isset($options['missing'])) {
    $selected = 'missing';
  }
  elseif (!isset($stored_settings['no_record'])) {
    $selected = $stored_settings[$type . '_display_plugin'];
  }
  $fieldset[$type . '_display_select'] = array(
    '#ajax' => array(
      'callback' => 'taxonomy_display_ajax_' . $type . '_display_callback',
      'wrapper' => 'replace-' . $type . '-display-form',
    ),
    '#default_value' => $selected,
    '#description' => $select_field_description,
    '#options' => $options,
    '#title' => $select_field_title,
    '#type' => 'select',
  );

  if (isset($form_state['taxonomy_display'][$type . '_display_handler'])) {
    $last_handler = $form_state['taxonomy_display'][$type . '_display_handler'];
  }
  $form_state['taxonomy_display'][$type . '_display_handler'] = $selected;

  if ($selected == 'missing') {
    $fieldset[$type . '_display_form'] = array(
      '#description' => t('The selected display plugin is missing. Either restore the plugin or select one that is available to configure.'),
      '#prefix' => '<div id="replace-' . $type . '-display-form">',
      '#suffix' => '</div>',
      '#type' => 'fieldset',
    );
    return;
  }

  $fieldset[$type . '_display_form'] = array(
    '#prefix' => '<div id="replace-' . $type . '-display-form">',
    '#suffix' => '</div>',
    '#title' => t('!label display options', array('!label' => $options[$selected])),
    '#type' => 'fieldset',
  );

  // Get the fieldset from the selected handler.
  $plugin_class = new $selected;
  // Set $options as null
  $display_options = NULL;
  if (!isset($stored_settings['no_record'])) {
    $display_options = $stored_settings[$type . '_display_options'];
  }
  // If the handler wasn't the one used on the last form build then reset the
  // $form_state values for it.
  if (!isset($last_handler) || $last_handler != $selected) {
    $values[$type . '_display_form'] = array();
  }
  $plugin_class->formFieldset($fieldset[$type . '_display_form'], $values[$type . '_display_form'], $display_options);
}