You are here

function taxonomy_multidelete_term_form_taxonomy_overview_terms_alter in Taxonomy Multi-delete Terms 7

Implements hook_form_FORM_ID_alter().

Alter taxonomy overview terms list.

File

./taxonomy_multidelete_term.module, line 141
Taxonomy multi delete module use to delete terms in bulk.

Code

function taxonomy_multidelete_term_form_taxonomy_overview_terms_alter(&$form, &$form_state, $form_id) {
  $form['#confirm_delete'] = 0;
  $vocabulary = $form_state['build_info']['args'][0];

  // Check for confirmation delete forms.
  if (isset($form_state['confirm_delete_terms'])) {
    hide($form['actions']);
    $form[] = taxonomy_multidelete_term_confirm_delete_terms($form, $form_state, $form_id);
    $form['#confirm_delete'] = 1;
    return $form;
  }

  // Check for confirmation forms.
  if (!isset($form_state['confirm_reset_alphabetical']) && !isset($form_state['confirm_delete_terms'])) {
    $page = isset($_GET['page']) ? $_GET['page'] : 0;
    $page_increment = variable_get('taxonomy_terms_per_page_admin', 100);
    $page_entries = 0;
    $before_entries = 0;
    $after_entries = 0;
    $root_entries = 0;
    $complete_tree = TRUE;

    // 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_step = NULL;
    $forward_step = 0;

    // An array of the terms to be displayed on this page.
    $current_page = array();
    $delta = 0;
    $term_deltas = array();
    $tree = taxonomy_get_tree($vocabulary->vid);
    $term = current($tree);
    do {

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

      // Count entries before the current page.
      if ($page && $page * $page_increment > $before_entries && !isset($back_step)) {
        $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_step)) {
        $back_step = 0;
        while ($pterm = prev($tree)) {
          $before_entries--;
          $back_step++;
          if ($pterm->depth == 0) {
            prev($tree);
            continue 2;
          }
        }
      }
      $back_step = isset($back_step) ? $back_step : 0;

      // Continue rendering the tree until we reach the a new root item.
      if ($page_entries >= $page_increment + $back_step + 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_step) {
        $forward_step++;
      }

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

    // Build the actual form.
    foreach ($current_page as $key => $term) {
      $form[$key]['view'] = array(
        '#type' => 'checkbox',
        '#title' => l($term->name, "taxonomy/term/{$term->tid}"),
      );
      $form['#parent_fields'] = TRUE;
      $form[$key]['tid'] = array(
        '#type' => 'hidden',
        '#value' => $term->tid,
      );
      $form[$key]['depth'] = array(
        '#type' => 'hidden',
        '#default_value' => $term->depth,
      );
      $form[$key]['weight'] = array(
        '#type' => 'weight',
        '#delta' => $delta,
        '#title_display' => 'invisible',
        '#title' => t('Weight for added term'),
        '#default_value' => $term->weight,
      );
    }
    if (count($tree) > 0) {
      $form['actions']['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete'),
        '#submit' => array(
          'taxonomy_multidelete_term_taxonomy_overview_terms_submit',
        ),
      );
      $form['#validate'][] = 'taxonomy_multidelete_term_taxonomy_overview_terms_validate';
    }
  }
}