taxonomy.admin.inc 22.8 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Administrative page callbacks for the taxonomy module.
 */

8 9
use Drupal\taxonomy\Plugin\Core\Entity\Term;
use Drupal\taxonomy\Plugin\Core\Entity\Vocabulary;
10

11
/**
12
 * Form builder to list and manage vocabularies.
13
 *
14
 * @ingroup forms
15 16
 * @see taxonomy_overview_vocabularies_submit()
 * @see theme_taxonomy_overview_vocabularies()
17
 */
18
function taxonomy_overview_vocabularies($form) {
19
  $vocabularies = taxonomy_vocabulary_load_multiple();
20
  taxonomy_vocabulary_sort($vocabularies);
21 22 23 24 25 26 27
  $form['vocabularies'] = array(
    '#type' => 'table',
    '#empty' => t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => url('admin/structure/taxonomy/add'))),
    '#attributes' => array(
      'id' => 'taxonomy',
    ),
  );
28
  foreach ($vocabularies as $vocabulary) {
29 30 31 32
    $form['vocabularies'][$vocabulary->id()]['#vocabulary'] = $vocabulary;
    $form['vocabularies'][$vocabulary->id()]['#attributes']['class'][] = 'draggable';
    $form['vocabularies'][$vocabulary->id()]['name'] = array('#markup' => check_plain($vocabulary->name));
    $form['vocabularies'][$vocabulary->id()]['weight'] = array(
33 34 35 36 37
      '#type' => 'weight',
      '#title' => t('Weight for @title', array('@title' => $vocabulary->name)),
      '#title_display' => 'invisible',
      '#delta' => 10,
      '#default_value' => $vocabulary->weight,
38 39 40
      '#attributes' => array(
        'class' => array('vocabulary-weight'),
      ),
41
    );
42 43 44
    $links = array();
    $links['edit'] = array(
      'title' => t('edit vocabulary'),
45
      'href' => "admin/structure/taxonomy/manage/{$vocabulary->id()}/edit",
46 47 48
    );
    $links['list'] = array(
      'title' => t('list terms'),
49
      'href' => "admin/structure/taxonomy/manage/{$vocabulary->id()}",
50 51 52
    );
    $links['add'] = array(
      'title' => t('add terms'),
53
      'href' => "admin/structure/taxonomy/manage/{$vocabulary->id()}/add",
54
    );
55
    $form['vocabularies'][$vocabulary->id()]['operations'] = array(
56 57 58
      '#type' => 'operations',
      '#links' => $links,
    );
59 60
  }

61 62
  $form['vocabularies']['#header'] = array(t('Vocabulary name'));

63 64 65
  // Only make this form include a submit button and weight if more than one
  // vocabulary exists.
  if (count($vocabularies) > 1) {
66 67
    $form['vocabularies']['#tabledrag'][] = array('order', 'sibling', 'vocabulary-weight');
    $form['vocabularies']['#header'][] = t('Weight');
68
    $form['actions'] = array('#type' => 'actions');
69
    $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#button_type' => 'primary');
70 71
  }
  elseif (isset($vocabulary)) {
72
    unset($form['vocabularies'][$vocabulary->id()]['weight']);
73
  }
74
  $form['vocabularies']['#header'][] = t('Operations');
75 76 77 78 79
  return $form;
}

/**
 * Submit handler for vocabularies overview. Updates changed vocabulary weights.
80
 *
81
 * @see taxonomy_overview_vocabularies()
82 83
 */
function taxonomy_overview_vocabularies_submit($form, &$form_state) {
84 85 86
  foreach ($form_state['values']['vocabularies'] as $vid => $vocabulary) {
    if (isset($form['vocabularies'][$vid]['#vocabulary']) && $form['vocabularies'][$vid]['#vocabulary']->weight != $form_state['values']['vocabularies'][$vid]['weight']) {
      $form['vocabularies'][$vid]['#vocabulary']->weight = $form_state['values']['vocabularies'][$vid]['weight'];
87
      $form['vocabularies'][$vid]['#vocabulary']->save();
88 89
    }
  }
90
  drupal_set_message(t('The configuration options have been saved.'));
91 92
}

93
/**
94
 * Page callback: provides the vocabulary creation form.
95
 */
96 97 98 99 100 101 102
function taxonomy_vocabulary_add() {
  $vocabulary = entity_create('taxonomy_vocabulary', array(
    // Default the new vocabulary to the site's default language. This is the
    // most likely default value until we have better flexible settings.
    'langcode' => language_default()->langcode,
  ));
  return entity_get_form($vocabulary);
103 104 105
}

/**
106
 * Form builder for the taxonomy terms overview.
107
 *
108
 * Display a tree of all the terms in a vocabulary, with options to edit
109
 * each one. The form is made drag and drop by the theme function.
110
 *
111
 * @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
112 113
 *   The taxonomy vocabulary entity to list terms for.
 *
114
 * @ingroup forms
115 116
 * @see taxonomy_overview_terms_submit()
 * @see theme_taxonomy_overview_terms()
117
 */
118
function taxonomy_overview_terms($form, &$form_state, Vocabulary $vocabulary) {
119
  global $pager_page_array, $pager_total, $pager_total_items;
120

121 122
  // Check for confirmation forms.
  if (isset($form_state['confirm_reset_alphabetical'])) {
123
    return taxonomy_vocabulary_confirm_reset_alphabetical($form, $form_state, $vocabulary->id());
124
  }
125

126
  $form_state['taxonomy']['vocabulary'] = $vocabulary;
127
  $parent_fields = FALSE;
128 129

  $page            = isset($_GET['page']) ? $_GET['page'] : 0;
130
  $page_increment  = config('taxonomy.settings')->get('terms_per_page_admin');  // Number of terms per page.
131 132 133 134 135 136 137 138
  $page_entries    = 0;   // Elements shown on this page.
  $before_entries  = 0;   // Elements at the root level before this page.
  $after_entries   = 0;   // Elements at the root level after this page.
  $root_entries    = 0;   // Elements at the root level on this page.

  // Terms from previous and next pages are shown if the term tree would have
  // been cut in the middle. Keep track of how many extra terms we show on each
  // page of terms.
139 140
  $back_step    = NULL;
  $forward_step = 0;
141

142 143 144
  // An array of the terms to be displayed on this page.
  $current_page = array();

145
  $delta = 0;
146
  $term_deltas = array();
147
  $tree = taxonomy_get_tree($vocabulary->id(), 0, NULL, TRUE);
148 149 150 151 152 153
  $term = current($tree);
  do {
    // In case this tree is completely empty.
    if (empty($term)) {
      break;
    }
154
    $delta++;
155
    // Count entries before the current page.
156
    if ($page && ($page * $page_increment) > $before_entries && !isset($back_step)) {
157 158 159 160 161 162 163
      $before_entries++;
      continue;
    }
    // Count entries after the current page.
    elseif ($page_entries > $page_increment && isset($complete_tree)) {
      $after_entries++;
      continue;
164
    }
165

166
    // Do not let a term start the page that is not at the root.
167 168
    if (isset($term->depth) && ($term->depth > 0) && !isset($back_step)) {
      $back_step = 0;
169 170
      while ($pterm = prev($tree)) {
        $before_entries--;
171
        $back_step++;
172 173 174 175
        if ($pterm->depth == 0) {
          prev($tree);
          continue 2; // Jump back to the start of the root level parent.
       }
176
      }
177
    }
178
    $back_step = isset($back_step) ? $back_step : 0;
179 180

    // Continue rendering the tree until we reach the a new root item.
181
    if ($page_entries >= $page_increment + $back_step + 1 && $term->depth == 0 && $root_entries > 1) {
182 183 184 185 186
      $complete_tree = TRUE;
      // This new item at the root level is the first item on the next page.
      $after_entries++;
      continue;
    }
187 188
    if ($page_entries >= $page_increment + $back_step) {
      $forward_step++;
189
    }
190

191 192
    // Finally, if we've gotten down this far, we're rendering a term on this page.
    $page_entries++;
193 194
    $term_deltas[$term->id()] = isset($term_deltas[$term->id()]) ? $term_deltas[$term->id()] + 1 : 0;
    $key = 'tid:' . $term->id() . ':' . $term_deltas[$term->id()];
195

196 197
    // Keep track of the first term displayed on this page.
    if ($page_entries == 1) {
198
      $form['#first_tid'] = $term->id();
199 200 201 202 203 204 205
    }
    // Keep a variable to make sure at least 2 root elements are displayed.
    if ($term->parents[0] == 0) {
      $root_entries++;
    }
    $current_page[$key] = $term;
  } while ($term = next($tree));
206

207 208 209 210 211
  // Because we didn't use a pager query, set the necessary pager variables.
  $total_entries = $before_entries + $page_entries + $after_entries;
  $pager_total_items[0] = $total_entries;
  $pager_page_array[0] = $page;
  $pager_total[0] = ceil($total_entries / $page_increment);
212 213 214

  // If this form was already submitted once, it's probably hit a validation
  // error. Ensure the form is rebuilt in the same order as the user submitted.
215
  if (!empty($form_state['input'])) {
216
    $order = array_flip(array_keys($form_state['input']['terms'])); // Get the $_POST order.
217 218
    $current_page = array_merge($order, $current_page); // Update our form with the new order.
    foreach ($current_page as $key => $term) {
219 220 221 222
      // Verify this is a term for the current page and set at the current
      // depth.
      if (is_array($form_state['input']['terms'][$key]) && is_numeric($form_state['input']['terms'][$key]['term']['tid'])) {
        $current_page[$key]->depth = $form_state['input']['terms'][$key]['term']['depth'];
223 224 225 226
      }
      else {
        unset($current_page[$key]);
      }
227
    }
228
  }
229 230

  $errors = form_get_errors() != FALSE ? form_get_errors() : array();
231
  $destination = drupal_get_destination();
232
  $row_position = 0;
233
  // Build the actual form.
234 235 236
  $form['terms'] = array(
    '#type' => 'table',
    '#header' => array(t('Name'), t('Weight'), t('Operations')),
237
    '#empty' => t('No terms available. <a href="@link">Add term</a>.', array('@link' => url('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add'))),
238 239 240 241
    '#attributes' => array(
      'id' => 'taxonomy',
    ),
  );
242
  foreach ($current_page as $key => $term) {
243
    $form['terms'][$key]['#term'] = $term;
244
    $form['terms'][$key]['term'] = array(
245
      '#prefix' => isset($term->depth->value) && $term->depth->value > 0 ? theme('indentation', array('size' => $term->depth->value)) : '',
246 247
      '#type' => 'link',
      '#title' => $term->label(),
248
      '#href' => "taxonomy/term/$term->id()",
249
    );
250
    if ($vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE && count($tree) > 1) {
251 252
      $parent_fields = TRUE;
      $form['terms'][$key]['term']['tid'] = array(
253
        '#type' => 'hidden',
254
        '#value' => $term->id(),
255 256 257
        '#attributes' => array(
          'class' => array('term-id'),
        ),
258
      );
259
      $form['terms'][$key]['term']['parent'] = array(
260
        '#type' => 'hidden',
261 262
        // Yes, default_value on a hidden. It needs to be changeable by the
        // javascript.
263
        '#default_value' => $term->parent->value,
264 265 266
        '#attributes' => array(
          'class' => array('term-parent'),
        ),
267
      );
268
      $form['terms'][$key]['term']['depth'] = array(
269
        '#type' => 'hidden',
270 271
        // Same as above, the depth is modified by javascript, so it's a
        // default_value.
272
        '#default_value' => $term->depth,
273 274 275
        '#attributes' => array(
          'class' => array('term-depth'),
        ),
276
      );
277
    }
278 279 280 281 282
    $form['terms'][$key]['weight'] = array(
      '#type' => 'weight',
      '#delta' => $delta,
      '#title_display' => 'invisible',
      '#title' => t('Weight for added term'),
283
      '#default_value' => $term->weight->value,
284 285 286 287
      '#attributes' => array(
        'class' => array('term-weight'),
      ),
    );
288
    $operations = array(
289 290
      'edit' => array(
        'title' => t('edit'),
291
        'href' => 'taxonomy/term/' . $term->id() . '/edit',
292 293 294 295
        'query' => $destination,
      ),
      'delete' => array(
        'title' => t('delete'),
296
        'href' => 'taxonomy/term/' . $term->id() . '/delete',
297 298
        'query' => $destination,
      ),
299
    );
300 301 302
    if (module_invoke('translation_entity', 'translate_access', $term)) {
      $operations['translate'] = array(
        'title' => t('translate'),
303
        'href' => 'taxonomy/term/' . $term->id() . '/translations',
304 305 306
        'query' => $destination,
      );
    }
307
    $form['terms'][$key]['operations'] = array(
308
      '#type' => 'operations',
309 310
      '#links' => $operations,
    );
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

    $form['terms'][$key]['#attributes']['class'] = array();
    if ($parent_fields) {
      $form['terms'][$key]['#attributes']['class'][] = 'draggable';
    }

    // Add classes that mark which terms belong to previous and next pages.
    if ($row_position < $back_step || $row_position >= $page_entries - $forward_step) {
      $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-preview';
    }

    if ($row_position !== 0 && $row_position !== count($tree) - 1) {
      if ($row_position == $back_step - 1 || $row_position == $page_entries - $forward_step - 1) {
        $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-top';
      }
      elseif ($row_position == $back_step || $row_position == $page_entries - $forward_step) {
        $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-bottom';
      }
    }

    // Add an error class if this row contains a form error.
    foreach ($errors as $error_key => $error) {
      if (strpos($error_key, $key) === 0) {
        $form['terms'][$key]['#attributes']['class'][] = 'error';
      }
    }
    $row_position++;
338
  }
339

340 341 342 343 344 345 346 347 348 349 350

  if ($parent_fields) {
    $form['terms']['#tabledrag'][] = array('match', 'parent', 'term-parent', 'term-parent', 'term-id', FALSE);
    $form['terms']['#tabledrag'][] = array('depth', 'group', 'term-depth', NULL, NULL, FALSE);
    $form['terms']['#attached']['library'][] = array('taxonomy', 'drupal.taxonomy');
    $form['terms']['#attached']['js'][] = array(
      'data' => array('taxonomy' => array('backStep' => $back_step, 'forwardStep' => $forward_step)),
      'type' => 'setting',
    );
  }
  $form['terms']['#tabledrag'][] = array('order', 'sibling', 'term-weight');
351

352
  if ($vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE && count($tree) > 1) {
353 354
    $form['actions'] = array('#type' => 'actions', '#tree' => FALSE);
    $form['actions']['submit'] = array(
355
      '#type' => 'submit',
356 357
      '#value' => t('Save'),
      '#button_type' => 'primary',
358
    );
359
    $form['actions']['reset_alphabetical'] = array(
360
      '#type' => 'submit',
361
      '#value' => t('Reset to alphabetical')
362
    );
363
    $form_state['redirect'] = array(current_path(), (isset($_GET['page']) ? array('query' => array('page' => $_GET['page'])) : array()));
364 365
  }

366
  return $form;
367 368
}

369 370
/**
 * Submit handler for terms overview form.
371
 *
372 373
 * Rather than using a textfield or weight field, this form depends entirely
 * upon the order of form elements on the page to determine new weights.
374
 *
375 376 377 378 379 380
 * Because there might be hundreds or thousands of taxonomy terms that need to
 * be ordered, terms are weighted from 0 to the number of terms in the
 * vocabulary, rather than the standard -10 to 10 scale. Numbers are sorted
 * lowest to highest, but are not necessarily sequential. Numbers may be skipped
 * when a term has children so that reordering is minimal when a child is
 * added or removed from a term.
381
 *
382
 * @see taxonomy_overview_terms()
383 384
 */
function taxonomy_overview_terms_submit($form, &$form_state) {
385
  if ($form_state['triggering_element']['#value'] == t('Reset to alphabetical')) {
386
    // Execute the reset action.
387 388 389
    if ($form_state['values']['reset_alphabetical'] === TRUE) {
      return taxonomy_vocabulary_confirm_reset_alphabetical_submit($form, $form_state);
    }
390
    // Rebuild the form to confirm the reset action.
391 392 393 394 395
    $form_state['rebuild'] = TRUE;
    $form_state['confirm_reset_alphabetical'] = TRUE;
    return;
  }

396
  // Sort term order based on weight.
397
  uasort($form_state['values']['terms'], 'drupal_sort_weight');
398

399
  $vocabulary = $form_state['taxonomy']['vocabulary'];
400 401
  // Update the current hierarchy type as we go.
  $hierarchy = TAXONOMY_HIERARCHY_DISABLED;
402 403

  $changed_terms = array();
404
  $tree = taxonomy_get_tree($vocabulary->id(), 0, NULL, TRUE);
405 406 407 408 409 410 411

  if (empty($tree)) {
    return;
  }

  // Build a list of all terms that need to be updated on previous pages.
  $weight = 0;
412 413 414 415 416
  $term = $tree[0];
  while ($term->id() != $form['#first_tid']) {
    if ($term->parent->value == 0 && $term->weight->value != $weight) {
      $term->weight->value = $weight;
      $changed_terms[$term->id()] = $term;
417 418
    }
    $weight++;
419 420
    $hierarchy = $term->parent->value != 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy;
    $term = $tree[$weight];
421 422 423 424
  }

  // Renumber the current page weights and assign any new parents.
  $level_weights = array();
425 426 427
  foreach ($form_state['values']['terms'] as $tid => $values) {
    if (isset($form['terms'][$tid]['#term'])) {
      $term = $form['terms'][$tid]['#term'];
428
      // Give terms at the root level a weight in sequence with terms on previous pages.
429 430 431
      if ($values['term']['parent'] == 0 && $term->weight->value != $weight) {
        $term->weight->value = $weight;
        $changed_terms[$term->id()] = $term;
432 433
      }
      // Terms not at the root level can safely start from 0 because they're all on this page.
434
      elseif ($values['term']['parent'] > 0) {
435 436 437 438
        $level_weights[$values['term']['parent']] = isset($level_weights[$values['term']['parent']]) ? $level_weights[$values['term']->parent->value] + 1 : 0;
        if ($level_weights[$values['term']['parent']] != $term->weight->value) {
          $term->weight->value = $level_weights[$values['term']['parent']];
          $changed_terms[$term->id()] = $term;
439 440 441
        }
      }
      // Update any changed parents.
442 443 444
      if ($values['term']['parent'] != $term->parent->value) {
        $term->parent->value = $values['term']['parent'];
        $changed_terms[$term->id()] = $term;
445
      }
446
      $hierarchy = $term->parent->value != 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy;
447 448 449 450 451 452
      $weight++;
    }
  }

  // Build a list of all terms that need to be updated on following pages.
  for ($weight; $weight < count($tree); $weight++) {
453 454 455 456 457
    $term = $tree[$weight];
    if ($term->parent->value == 0 && $term->weight->value != $weight) {
      $term->parent->value = $term->parent->value;
      $term->weight->value = $weight;
      $changed_terms[$term->id()] = $term;
458
    }
459
    $hierarchy = $term->parent->value != 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy;
460 461 462
  }

  // Save all updated terms.
463 464
  foreach ($changed_terms as $term) {
    $term->save();
465 466 467
  }

  // Update the vocabulary hierarchy to flat or single hierarchy.
468 469
  if ($vocabulary->hierarchy != $hierarchy) {
    $vocabulary->hierarchy = $hierarchy;
470
    $vocabulary->save();
471
  }
472
  drupal_set_message(t('The configuration options have been saved.'));
473 474
}

475
/**
476
 * Returns a rendered edit form to create a new term associated to the given vocabulary.
477
 */
478
function taxonomy_term_add($vocabulary) {
479
  $term = entity_create('taxonomy_term', array('vid' => $vocabulary->id()));
480
  if (module_exists('language')) {
481
    $term->langcode = language_get_default_langcode('taxonomy_term', $vocabulary->id());
482
  }
483
  return entity_get_form($term);
484 485
}

486 487 488 489
/**
 * Form builder for the term delete form.
 *
 * @ingroup forms
490
 * @see taxonomy_term_confirm_delete_submit()
491
 */
492
function taxonomy_term_confirm_delete($form, &$form_state, Term $term) {
493
  // Always provide entity id in the same form key as in the entity edit form.
494
  $form['tid'] = array('#type' => 'value', '#value' => $term->id());
495

496
  $form_state['taxonomy']['vocabulary'] = taxonomy_vocabulary_load($term->bundle());
497
  $form['type'] = array('#type' => 'value', '#value' => 'term');
498
  $form['name'] = array('#type' => 'value', '#value' => $term->label());
499
  $form['vid'] = array('#type' => 'value', '#value' => $term->bundle());
500
  $form['delete'] = array('#type' => 'value', '#value' => TRUE);
501
  return confirm_form($form,
502
    t('Are you sure you want to delete the term %title?',
503
      array('%title' => $term->label())),
504 505 506 507
    'admin/structure/taxonomy',
    t('Deleting a term will delete all its children if there are any. This action cannot be undone.'),
    t('Delete'),
    t('Cancel'));
508 509
}

510 511
/**
 * Submit handler to delete a term after confirmation.
512
 *
513
 * @see taxonomy_term_confirm_delete()
514
 */
515
function taxonomy_term_confirm_delete_submit($form, &$form_state) {
516
  entity_delete_multiple('taxonomy_term', array($form_state['values']['tid']));
517
  taxonomy_check_vocabulary_hierarchy($form_state['taxonomy']['vocabulary'], $form_state['values']);
518
  drupal_set_message(t('Deleted term %name.', array('%name' => $form_state['values']['name'])));
519
  watchdog('taxonomy', 'Deleted term %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
520 521 522
  if (!isset($_GET['destination'])) {
    $form_state['redirect'] = 'admin/structure/taxonomy';
  }
523
  cache_invalidate_tags(array('content' => TRUE));
524 525 526 527 528 529 530
  return;
}

/**
 * Form builder for the vocabulary delete confirmation form.
 *
 * @ingroup forms
531
 * @see taxonomy_vocabulary_confirm_delete_submit()
532
 */
533
function taxonomy_vocabulary_confirm_delete($form, &$form_state, $vid) {
534 535
  $vocabulary = taxonomy_vocabulary_load($vid);

536 537 538
  // Always provide entity id in the same form key as in the entity edit form.
  $form['vid'] = array('#type' => 'value', '#value' => $vid);

539
  $form_state['taxonomy']['vocabulary'] = $vocabulary;
540
  $form['#id'] = 'taxonomy_vocabulary_confirm_delete';
541 542
  $form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
  $form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
543
  $form['#submit'] = array('taxonomy_vocabulary_confirm_delete_submit');
544
  return confirm_form($form,
545
    t('Are you sure you want to delete the vocabulary %title?',
546
      array('%title' => $vocabulary->label())),
547 548 549 550
    'admin/structure/taxonomy',
    t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'),
    t('Delete'),
    t('Cancel'));
551 552
}

553 554
/**
 * Submit handler to delete a vocabulary after confirmation.
555
 *
556
 * @see taxonomy_vocabulary_confirm_delete()
557
 */
558
function taxonomy_vocabulary_confirm_delete_submit($form, &$form_state) {
559
  $form_state['taxonomy']['vocabulary']->delete();
560
  drupal_set_message(t('Deleted vocabulary %name.', array('%name' => $form_state['values']['name'])));
561
  watchdog('taxonomy', 'Deleted vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
562
  $form_state['redirect'] = 'admin/structure/taxonomy';
563
  cache_invalidate_tags(array('content' => TRUE));
564 565
  return;
}
566 567

/**
568
 * Form builder to confirm resetting a vocabulary to alphabetical order.
569
 *
570
 * @ingroup forms
571
 * @see taxonomy_vocabulary_confirm_reset_alphabetical_submit()
572
 */
573
function taxonomy_vocabulary_confirm_reset_alphabetical($form, &$form_state, $vid) {
574 575 576 577 578 579 580
  $vocabulary = taxonomy_vocabulary_load($vid);

  $form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
  $form['vid'] = array('#type' => 'value', '#value' => $vid);
  $form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
  $form['reset_alphabetical'] = array('#type' => 'value', '#value' => TRUE);
  return confirm_form($form,
581 582
    t('Are you sure you want to reset the vocabulary %title to alphabetical order?',
      array('%title' => $vocabulary->label())),
583
    'admin/structure/taxonomy/manage/' . $vocabulary->id(),
584 585 586
    t('Resetting a vocabulary will discard all custom ordering and sort items alphabetically.'),
    t('Reset to alphabetical'),
    t('Cancel'));
587 588 589 590
}

/**
 * Submit handler to reset a vocabulary to alphabetical order after confirmation.
591
 *
592
 * @see taxonomy_vocabulary_confirm_reset_alphabetical()
593 594
 */
function taxonomy_vocabulary_confirm_reset_alphabetical_submit($form, &$form_state) {
595 596 597 598
  db_update('taxonomy_term_data')
    ->fields(array('weight' => 0))
    ->condition('vid', $form_state['values']['vid'])
    ->execute();
599
  drupal_set_message(t('Reset vocabulary %name to alphabetical order.', array('%name' => $form_state['values']['name'])));
600
  watchdog('taxonomy', 'Reset vocabulary %name to alphabetical order.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
601
  $form_state['redirect'] = 'admin/structure/taxonomy/manage/' . $form_state['values']['vid'];
602
}