1 taxonomy.admin.inc | taxonomy_overview_terms($form, &$form_state, TaxonomyVocabulary $vocabulary) |
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.
Parameters
TaxonomyVocabulary $vocabulary: The taxonomy vocabulary entity to list terms for.
See also
taxonomy_overview_terms_submit()
theme_taxonomy_overview_terms()
Related topics
File
- core/
modules/ taxonomy/ taxonomy.admin.inc, line 422 - Admin page callbacks for the Taxonomy module.
Code
function taxonomy_overview_terms($form, &$form_state, TaxonomyVocabulary $vocabulary) {
global $pager_page_array, $pager_total, $pager_total_items, $language;
// Check for confirmation forms.
if (isset($form_state['confirm_reset_alphabetical'])) {
return taxonomy_vocabulary_confirm_reset_alphabetical($form, $form_state, $vocabulary->machine_name);
}
$filter_langcode = isset($_GET['langcode_term']) ? $_GET['langcode_term'] : $language->langcode;
if (module_exists('language') && $vocabulary->language == TAXONOMY_LANGUAGE_ENABLED) {
$form['langcode_term'] = array(
'#title' => t('Show terms in language'),
'#type' => 'select',
'#options' => language_list(TRUE, TRUE),
'#empty_value' => LANGUAGE_NONE,
'#empty_option' => t('All languages'),
'#default_value' => $filter_langcode,
'#access' => module_exists('language') && $vocabulary->language == TAXONOMY_LANGUAGE_ENABLED,
'#weight' => -20,
'#description' => t('Show terms for a given language. Terms with the "All" language will be shown in every language.'),
);
$form['langcode_filter'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
'#attributes' => array('class' => array('button-primary')),
'#submit' => array('term_overview_form_language_filter_submit'),
);
}
$form['#vocabulary'] = $vocabulary;
$form['#tree'] = TRUE;
$form['#parent_fields'] = FALSE;
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$page_increment = config_get('taxonomy.settings', 'terms_per_page_admin'); // 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_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->machine_name);
$term = current($tree);
do {
// In case this tree is completely empty.
if (empty($term)) {
break;
}
// Skip all terms not in the requested language.
$tree_langcode = $filter_langcode === LANGUAGE_NONE ? NULL : $filter_langcode;
if ($tree_langcode && $term->langcode !== $tree_langcode && $term->langcode !== LANGUAGE_NONE) {
continue;
}
$delta++;
// Count entries before the current page.
if ($page && ($page * $page_increment) > $before_entries && !isset($back_step)) {
$before_entries++;
continue;
}
// Count entries after the current page.
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 ($previous_term = prev($tree)) {
$before_entries--;
$back_step++;
if ($previous_term->depth == 0) {
prev($tree);
continue 2; // Jump back to the start of the root level parent.
}
}
}
$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));
// 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['input'])) {
$order = array_flip(array_keys($form_state['input'])); // 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['input'][$key]) && is_numeric($form_state['input'][$key]['tid'])) {
$current_page[$key]->depth = $form_state['input'][$key]['depth'];
}
else {
unset($current_page[$key]);
}
}
}
$language_options = language_list(TRUE, TRUE);
$language_options[LANGUAGE_NONE] = t('All');
// 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('#type' => 'link', '#title' => $term->name, '#href' => "taxonomy/term/$term->tid");
if ($vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE && 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,
);
// Language
$langcode_label = isset($language_options[$term->langcode]) ? $language_options[$term->langcode] : $term->langcode;
if (module_exists('language') && $vocabulary->language == TAXONOMY_LANGUAGE_ENABLED) {
$form[$key]['langcode'] = array(
'#type' => 'markup',
'#markup' => check_plain($langcode_label),
'#access' => module_exists('language') && $vocabulary->language == TAXONOMY_LANGUAGE_ENABLED,
);
}
if (taxonomy_vocabulary_access('update', $vocabulary)) {
$form[$key]['weight'] = array(
'#type' => 'weight',
'#delta' => $delta,
'#title_display' => 'invisible',
'#title' => t('Weight for added term'),
'#default_value' => $term->weight,
);
}
}
$links = array();
if (taxonomy_vocabulary_access('update', $vocabulary)) {
$links['edit'] = array(
'title' => t('Edit'),
'href' => 'taxonomy/term/' . $term->tid . '/edit',
'query' => backdrop_get_destination(),
);
}
if (taxonomy_vocabulary_access('delete', $vocabulary)) {
$links['delete'] = array(
'title' => t('Delete'),
'href' => 'taxonomy/term/' . $term->tid . '/delete',
'query' => backdrop_get_destination(),
);
}
$form[$key]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
}
$form['#total_entries'] = $total_entries;
$form['#page_increment'] = $page_increment;
$form['#page_entries'] = $page_entries;
$form['#back_step'] = $back_step;
$form['#forward_step'] = $forward_step;
$form['#empty_text'] = t('No terms available.');
if (taxonomy_vocabulary_access('create', $vocabulary)) {
$form['#empty_text'] = t('No terms available. <a href="@link">Add term</a>.', array('@link' => url('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add')));
}
if ($vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE && count($tree) > 1 && taxonomy_vocabulary_access('edit', $vocabulary)) {
$form['actions'] = array('#type' => 'actions', '#tree' => FALSE);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
$form['actions']['reset_alphabetical'] = array(
'#type' => 'submit',
'#value' => t('Reset to alphabetical')
);
$form_state['redirect'] = array($_GET['q'], (isset($_GET['page']) ? array('query' => array('page' => $_GET['page'])) : array()));
}
return $form;
}