Hi there,

I am developing small module for term disabling feature (one of my customers asked for it). This is what I have done so far:

There is a new table named taxonomy_term_disabled having two columns - id, tid and it only contains terms which are disabled (so enabling term actually means deleting record from the table, disabling means adding one to it). I also changed the term edit form, so it now contains checkbox for disabling term. So far it was quite easy, but now I need to expose this value to Views. I went through some API documentation and I know roughly which files I have to create but I could not find out where and how exactly shall I put the main "brain" of this feature, the main piece of code which says - when showing terms in Views, provide an information about presence of a row for the particular tid in taxonomy_term_disable table.

Would someone be able to help? Thanks a lot in advance.

Btw.: I am a bit surprised that this feature is not part of Drupal core and as I went through forums I could not find any related topic. Is it just me or is this really that special feature or am I missing something? :-)

Comments

drclaw’s picture

Yeah, getting started with exposing your module data to views is a bit tricky. First thing you need to do is create a file that goes by the naming convention [module_name].views.inc and put that in your module folder. I prefer to put this file in a sub folder called 'views'. Next, implement hook_views_api in your.module file

/**
 * Implementation of hook_views_api().
 */
function YOUR_MODULE_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'YOUR_MODULE') .'/views', // This is the path to the folder that has the modulename.views.inc file
  );
}

The modulename.views.inc file just needs one hook to expose the data to views which is hook_views_data(). Something like this should get you started:

/**
 * Implements hook_views_data().
 */
function time_tracker_views_data() {
$data['taxonomy_term_disabled']['table']['join']['taxonomy'] = array(
  'left_field' => 'tid',
  'field' => 'tid',
);
$data['taxonomy_term_disabled']['table']['group'] = t('Taxonomy');
$data['taxonomy_term_disabled']['id'] = array(
  'title' => t('Disabled'),
  'help' => t('Is this taxonomy term flagged as disabled?'),
  'field' => array(
    'handler' => 'views_handler_field',
  ),
  'sort' => array(
    'handler' => 'views_handler_sort_sort',
  ),
  'filter' => array(
    'handler' => 'views_handler_filter_numeric',
  ),
  'argument' => array(
    'handler' => 'views_handler_argument_numeric',
  ),
);
}

This should join your table on the taxonomy id and give you access to the 'id' column. You might also be able to do this:

$data['taxonomy_term_disabled']['id'] = array(
  'title' => t('Disabled'),
  'help' => t('Is this taxonomy term flagged as disabled?'),
  'field' => array(
    'handler' => 'views_handler_field_boolean',
  ),
  'sort' => array(
    'handler' => 'views_handler_sort',
  ),
  'filter' => array(
    'handler' => 'views_handler_filter_boolean',
  ),
  'argument' => array(
    'handler' => 'views_handler_argument',
  ),
);

Where you use a boolean handler instead and treat the id column as a quasi true/false... although I'm not sure if that will work