Same name and namespace in other branches
  1. 4.7.x modules/taxonomy.module \taxonomy_overview_terms()
  2. 5.x modules/taxonomy/taxonomy.module \taxonomy_overview_terms()
  3. 7.x modules/taxonomy/taxonomy.admin.inc \taxonomy_overview_terms()

Form builder for the taxonomy terms overview.

Display a tree of all the terms in a vocabulary, with options to edit each one. The form is made drag and drop by the theme function.

See also

taxonomy_overview_terms_submit()

theme_taxonomy_overview_terms()

Related topics

1 call to taxonomy_overview_terms()
forum_overview in modules/forum/forum.admin.inc
Returns an overview list of existing forums and containers
2 string references to 'taxonomy_overview_terms'
forum_overview in modules/forum/forum.admin.inc
Returns an overview list of existing forums and containers
taxonomy_menu in modules/taxonomy/taxonomy.module
Implementation of hook_menu().

File

modules/taxonomy/taxonomy.admin.inc, line 245
Administrative page callbacks for the taxonomy module.

Code

function taxonomy_overview_terms(&$form_state, $vocabulary) {
  global $pager_page_array, $pager_total, $pager_total_items;

  // Check for confirmation forms.
  if (isset($form_state['confirm_reset_alphabetical'])) {
    return taxonomy_vocabulary_confirm_reset_alphabetical($form_state, $vocabulary->vid);
  }
  drupal_set_title(t('Terms in %vocabulary', array(
    '%vocabulary' => $vocabulary->name,
  )));
  $form = array(
    '#vocabulary' => (array) $vocabulary,
    '#tree' => TRUE,
    '#parent_fields' => FALSE,
  );
  $page = isset($_GET['page']) ? $_GET['page'] : 0;
  $page_increment = variable_get('taxonomy_terms_per_page_admin', 100);

  // Number of terms per page.
  $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.
  $back_peddle = NULL;
  $forward_peddle = 0;

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

  // Case for free tagging.
  if ($vocabulary->tags) {

    // We are not calling taxonomy_get_tree because that might fail with a big
    // number of tags in the freetagging vocabulary.
    $results = pager_query(db_rewrite_sql('SELECT t.*, h.parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $page_increment, 0, NULL, $vocabulary->vid);
    $total_entries = db_query(db_rewrite_sql('SELECT count(*) FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d', 't', 'tid'), $page_increment, 0, NULL, $vocabulary->vid);
    while ($term = db_fetch_object($results)) {
      $key = 'tid:' . $term->tid . ':0';
      $current_page[$key] = $term;
      $page_entries++;
    }
  }
  else {
    $term_deltas = array();
    $tree = taxonomy_get_tree($vocabulary->vid);
    $term = current($tree);
    do {

      // In case this tree is completely empty.
      if (empty($term)) {
        break;
      }

      // Count entries before the current page.
      if ($page && $page * $page_increment > $before_entries && !isset($back_peddle)) {
        $before_entries++;
        continue;
      }
      elseif ($page_entries > $page_increment && isset($complete_tree)) {
        $after_entries++;
        continue;
      }

      // Do not let a term start the page that is not at the root.
      if (isset($term->depth) && $term->depth > 0 && !isset($back_peddle)) {
        $back_peddle = 0;
        while ($pterm = prev($tree)) {
          $before_entries--;
          $back_peddle++;
          if ($pterm->depth == 0) {
            prev($tree);
            continue 2;

            // Jump back to the start of the root level parent.
          }
        }
      }
      $back_peddle = isset($back_peddle) ? $back_peddle : 0;

      // Continue rendering the tree until we reach the a new root item.
      if ($page_entries >= $page_increment + $back_peddle + 1 && $term->depth == 0 && $root_entries > 1) {
        $complete_tree = TRUE;

        // This new item at the root level is the first item on the next page.
        $after_entries++;
        continue;
      }
      if ($page_entries >= $page_increment + $back_peddle) {
        $forward_peddle++;
      }

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

      // Keep track of the first term displayed on this page.
      if ($page_entries == 1) {
        $form['#first_tid'] = $term->tid;
      }

      // 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));

    // 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);
  }

  // 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.
  if (!empty($form_state['post'])) {
    $order = array_flip(array_keys($form_state['post']));

    // Get the $_POST order.
    $current_page = array_merge($order, $current_page);

    // Update our form with the new order.
    foreach ($current_page as $key => $term) {

      // Verify this is a term for the current page and set at the current depth.
      if (is_array($form_state['post'][$key]) && is_numeric($form_state['post'][$key]['tid'])) {
        $current_page[$key]->depth = $form_state['post'][$key]['depth'];
      }
      else {
        unset($current_page[$key]);
      }
    }
  }

  // Build the actual form.
  foreach ($current_page as $key => $term) {

    // Save the term for the current page so we don't have to load it a second time.
    $form[$key]['#term'] = (array) $term;
    if (isset($term->parents)) {
      $form[$key]['#term']['parent'] = $term->parent = $term->parents[0];
      unset($form[$key]['#term']['parents'], $term->parents);
    }
    $form[$key]['view'] = array(
      '#value' => l($term->name, "taxonomy/term/{$term->tid}"),
    );
    if (!$vocabulary->tags && $vocabulary->hierarchy < 2 && count($tree) > 1) {
      $form['#parent_fields'] = TRUE;
      $form[$key]['tid'] = array(
        '#type' => 'hidden',
        '#value' => $term->tid,
      );
      $form[$key]['parent'] = array(
        '#type' => 'hidden',
        // Yes, default_value on a hidden. It needs to be changeable by the javascript.
        '#default_value' => $term->parent,
      );
      $form[$key]['depth'] = array(
        '#type' => 'hidden',
        // Same as above, the depth is modified by javascript, so it's a default_value.
        '#default_value' => $term->depth,
      );
    }
    $form[$key]['edit'] = array(
      '#value' => l(t('edit'), "admin/content/taxonomy/edit/term/{$term->tid}", array(
        'query' => drupal_get_destination(),
      )),
    );
  }
  $form['#total_entries'] = $total_entries;
  $form['#page_increment'] = $page_increment;
  $form['#page_entries'] = $page_entries;
  $form['#back_peddle'] = $back_peddle;
  $form['#forward_peddle'] = $forward_peddle;
  $form['#empty_text'] = t('No terms available.');
  if (!$vocabulary->tags && $vocabulary->hierarchy < 2 && count($tree) > 1) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    $form['reset_alphabetical'] = array(
      '#type' => 'submit',
      '#value' => t('Reset to alphabetical'),
    );
    $form['destination'] = array(
      '#type' => 'hidden',
      '#value' => $_GET['q'] . (isset($_GET['page']) ? '?page=' . $_GET['page'] : ''),
    );
  }
  return $form;
}