Extend Drupal's default Taxonomy terms overview table listing

Sharing something I recently needed to do.

I wanted to add a new column to the default Drupal's Taxonomy term listing page that provides a link to a DraggableViews (receives term ID from contextual filter) for sorting nodes.

This guide will show you briefly on how to make use of hook_theme().

See screenshot below for the final result.

Extended Drupal Taxonomy term table listing

In your custom module, add the following code:

/**
* Implements hook_form_FORM_ID_alter().
*/
function YOUR_MODULE_form_taxonomy_overview_terms_alter(&$form, &$form_state, $form_id) {
  // Add custom theme wrapper.
  array_unshift($form['#theme'], 'YOUR_MODULE_taxonomy_overview_terms');
}

/**
* Implements hook_theme().
*/
function YOUR_MODULE_theme() {
  return array(
    'YOUR_MODULE_taxonomy_overview_terms' => array(
      'render element' => 'form',
    ),
  );
}

function theme_YOUR_MODULE_taxonomy_overview_terms($vars) {
  ...
  // Codes from theme_taxonomy_overview_terms()
 
  foreach (element_children($form) as $key) {
    if (isset($form[$key]['#term'])) {
      $term = &$form[$key];

      $row = array();
      $row[] = (isset($term['#term']['depth']) && $term['#term']['depth'] > 0 ? theme('indentation', array('size' => $term['#term']['depth'])) : '') . drupal_render($term['view']);
      if ($form['#parent_fields']) {
        $term['tid']['#attributes']['class'] = array('term-id');
        $term['parent']['#attributes']['class'] = array('term-parent');
        $term['depth']['#attributes']['class'] = array('term-depth');
        $row[0] .= drupal_render($term['parent']) . drupal_render($term['tid']) . drupal_render($term['depth']);
      }
      $term['weight']['#attributes']['class'] = array('term-weight');
      $row[] = drupal_render($term['weight']);
      $row[] = drupal_render($term['edit']);
      $row[] = (isset($term['#term']['tid'])) ? l(t('sort'), 'blablabla/' . $term['#term']['tid']) : '';
      $row = array('data' => $row);
      $rows[$key] = $row;
    }
  }

  ...
}

Hope this short and simple snippet helps someone out there :)

1 comment for 'Extend Drupal's default Taxonomy terms overview table listing'

Add new comment