You are here

content_taxonomy_views.module in Content Taxonomy 5

Views Support for content_taxonomy

File

content_taxonomy_views.module
View source
<?php

/**
 * @file
 * Views Support for content_taxonomy
 **/

/**
 * Implementation of hook_help().
 **/
function content_taxonomy_views_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Views support for content_taxonomy. <em>Note: Requires views.module.</em>');
  }
}
function content_taxonomy_views_views_tables() {
  $tables = array();
  $fields = content_fields();
  $info = _content_type_info();
  foreach ($fields as $field) {
    if ($field['type'] == 'content_taxonomy') {

      //get out parent for opt groups
      foreach ($info['content types'] as $content_type) {
        foreach ($content_type['fields'] as $field_name => $field_info) {
          if ($field_name == $field['field_name']) {
            if (isset($field_info['widget']['group_tid'])) {
              $group_tid = $field_info['widget']['group_tid'];
            }
          }
        }
      }
      if ($field['save'] != 'tag') {
        $db_info = content_database_info($field);
        $tablename = $db_info['table'];
        $fieldname = $field['field_name'] . '_value';
        $table['name'] = $db_info['table'];
        $table['join'] = array(
          'left' => array(
            'table' => 'node',
            'field' => 'vid',
          ),
          'right' => array(
            'field' => 'vid',
          ),
        );
      }
      else {
        $tablename = 'content_taxonomy_' . $field['field_name'];
        $table['name'] = 'term_node';
        $fieldname = "tid";
        $table['join'] = array(
          'left' => array(
            'table' => 'node',
            'field' => 'nid',
          ),
          'right' => array(
            'field' => 'nid',
          ),
        );
      }
      $tables[$tablename] = array();
      $tables[$tablename] = $table;
      $tables[$tablename] += array(
        'provider' => 'internal',
        'fields' => array(
          $fieldname => array(
            'name' => 'Content Taxonomy Field: ' . $field['field_name'],
            'sortable' => TRUE,
            'parent' => $field['tid'],
            'voc_id' => $field['vid'],
            'content_field' => $field,
            'content_db_info' => $db_info,
          ),
        ),
        'filters' => array(
          $fieldname => array(
            'name' => 'Content Taxonomy Field: ' . $field['field_name'],
            'list' => 'views_handler_filter_terms_by_parent',
            'list-type' => isset($field['multiple']) ? 'list' : 'select',
            'value-type' => 'array',
            'option' => 'string',
            'operator' => 'views_handler_operator_andor',
            'parent' => $field['tid'],
            'voc_id' => $field['vid'],
            'group' => $group_tid,
          ),
        ),
      );
      if ($field['save'] == 'tag') {
        $tables[$tablename]['filters'][$fieldname]['handler'] = "views_handler_filter_tid_custom";
        $tables[$tablename]['fields'][$fieldname]['handler'] = array(
          'content_taxonomy_views_field_terms_by_parent' => t('Show value'),
        );
        $tables[$tablename]['fields'][$fieldname]['notafield'] = TRUE;
      }
      else {
        $tables[$tablename]['fields'][$fieldname]['handler'] = array(
          'content_taxonomy_views_field_handler_group' => t('Group multiple values'),
          'content_taxonomy_views_field_handler_ungroup' => t('Do not group multiple values'),
        );
      }
    }
  }
  return $tables;
}

/**
 * Implementation of hook_views_arguments()
 **/
function content_taxonomy_views_views_arguments() {
  $arguments = array();
  $fields = content_fields();
  foreach ($fields as $field) {
    if ($field['type'] == 'content_taxonomy' && $field['save'] != 'tag') {
      $arguments += array(
        'taxid_' . $field['field_name'] => array(
          'name' => 'Content Taxonomy Field: Term ID: ' . $field['field_name'],
          'handler' => 'content_taxonomy_views_argument_handler_taxid',
          'option' => 'string',
          'help' => t('The argument will filter by a taxonomy term ID. For this argument, set the option to the depth to search. (Does not support summary, sort and link)'),
        ),
        'taxletter_' . $field['field_name'] => array(
          'name' => 'Content Taxonomy Field: Term Name: ' . $field['field_name'],
          'handler' => 'content_taxonomy_views_argument_handler_taxletter',
          'option' => 'string',
          'help' => t('The argument will filter by a taxonomy term name. For this argument, set the option to the depth to search. (Does not support summary, sort and link)'),
        ),
      );
    }
  }
  return $arguments;
}

/**
 * Perform filtering by an argument (as term id) for field data stored via content taxonomy 
 */
function content_taxonomy_views_argument_handler_taxid($op, &$query, $argtype, $arg = '') {
  if ($op == 'filter') {
    $field_name = substr($argtype['type'], 6);
  }
  else {
    $field_name = substr($argtype, 6);
  }
  $field = content_fields($field_name);
  $db_info = content_database_info($field);
  $main_column = reset($db_info['columns']);

  // The table name used here is the Views alias for the table, not the actual
  // table name.
  $table = 'node_data_' . $field['field_name'];
  switch ($op) {
    case 'summary':
      $query
        ->ensure_table($table);
      $query
        ->add_field($main_column['column'], $table);
      return array(
        'field' => $table . '.' . $main_column['column'],
      );
      break;
    case 'sort':
      break;
    case 'filter':
      $query
        ->ensure_table($table);
      $column_placeholder = '%d';
      if ($argtype['options'] == '-1') {
        $th_num = $query
          ->add_table('term_hierarchy', false, 1, array(
          'left' => array(
            'table' => $table,
            'field' => $main_column['column'],
          ),
          'right' => array(
            'field' => 'parent',
          ),
        ));
        $th_table = $query
          ->get_table_name('term_hierarchy', $th_num);
        $query
          ->add_where($th_table . '.tid = %d', $arg);
      }
      else {
        if ($argtype['options'] == '1') {
          $th_num = $query
            ->add_table('term_hierarchy', false, 1, array(
            'left' => array(
              'table' => $table,
              'field' => $main_column['column'],
            ),
            'right' => array(
              'field' => 'tid',
            ),
          ));
          $th_table = $query
            ->get_table_name('term_hierarchy', $th_num);
          $query
            ->add_where($th_table . '.parent = %d', $arg);
        }
        else {
          $query
            ->add_where($table . '.' . $main_column['column'] . ' = ' . $column_placeholder, $arg);
        }
      }
      break;
    case 'link':
      break;
    case 'title':
      $item = array(
        key($db_info['columns']) => $query,
      );
      return content_format($field, $item, 'plain');
  }
}

/**
 * Perform filtering by an argument (as term name) for field data stored via content taxonomy 
 */
function content_taxonomy_views_argument_handler_taxletter($op, &$query, $argtype, $arg = '') {
  if ($op == 'filter') {
    $field_name = substr($argtype['type'], 10);
  }
  else {
    $field_name = substr($argtype, 10);
  }
  $field = content_fields($field_name);
  $db_info = content_database_info($field);
  $main_column = reset($db_info['columns']);

  // The table name used here is the Views alias for the table, not the actual
  // table name.
  $table = 'node_data_' . $field['field_name'];
  switch ($op) {
    case 'summary':
      $query
        ->ensure_table($table);
      $query
        ->add_field($main_column['column'], $table);
      return array(
        'field' => $table . '.' . $main_column['column'],
      );
      break;
    case 'sort':
      break;
    case 'filter':
      $query
        ->ensure_table($table);
      if ($argtype['options'] == '-1') {
        $th_num = $query
          ->add_table('term_hierarchy', false, 1, array(
          'left' => array(
            'table' => $table,
            'field' => $main_column['column'],
          ),
          'right' => array(
            'field' => 'parent',
          ),
        ));
        $th_table = $query
          ->get_table_name('term_hierarchy', $th_num);
        $td_num = $query
          ->add_table('term_data', false, 1, array(
          'left' => array(
            'table' => $th_table,
            'field' => 'tid',
          ),
          'right' => array(
            'field' => 'tid',
          ),
        ));
        $td_table = $query
          ->get_table_name('term_data', $td_num);
        $query
          ->add_where($td_table . ".name = '%s'", $arg);
      }
      else {
        if ($argtype['options'] == '1') {
          $th_num = $query
            ->add_table('term_hierarchy', false, 1, array(
            'left' => array(
              'table' => $table,
              'field' => $main_column['column'],
            ),
            'right' => array(
              'field' => 'tid',
            ),
          ));
          $th_table = $query
            ->get_table_name('term_hierarchy', $th_num);
          $td_num = $query
            ->add_table('term_data', false, 1, array(
            'left' => array(
              'table' => $th_table,
              'field' => 'parent',
            ),
            'right' => array(
              'field' => 'tid',
            ),
          ));
          $td_table = $query
            ->get_table_name('term_data', $td_num);
          $query
            ->add_where($td_table . ".name = '%s'", $arg);
        }
        else {
          $td_num = $query
            ->add_table('term_data', false, 1, array(
            'left' => array(
              'table' => $table,
              'field' => $main_column['column'],
            ),
            'right' => array(
              'field' => 'tid',
            ),
          ));
          $td_table = $query
            ->get_table_name('term_data', $td_num);
          $query
            ->add_where($td_table . ".name = '%s'", $arg);
        }
      }
      break;
    case 'link':
      break;
    case 'title':
      return check_plain($arg);
  }
}

/**
 * Display all the terms for a given parent term
 */
function content_taxonomy_views_field_terms_by_parent($fieldinfo, $fielddata, $value, $data) {
  $terms = array();
  $names = array();
  if ($fieldinfo['parent']) {
    $terms = content_taxonomy_terms_by_field(node_load($data->nid), $fieldinfo['voc_id'], $fieldinfo['parent'], 1);
  }
  if (is_array($terms)) {
    foreach ($terms as $term) {
      $names[] = $term->name;
    }
    return implode(', ', $names);
  }
}
function content_taxonomy_views_field_handler_group($fieldinfo, $fielddata, $value, $data) {
  if ($fieldinfo['content_field']['multiple']) {
    $fielddata['options'] = 'default';
    $result = content_views_field_handler_group($fieldinfo, $fielddata, $value, $data);
    return $result;
  }
  else {
    return content_taxonomy_views_field_handler_group($fieldinfo, $fielddata, $value, $data);
  }
}
function content_taxonomy_views_field_handler_ungroup($fieldinfo, $fielddata, $value, $data) {
  $term = taxonomy_get_term($value);
  return $term->name;
}

/**
 * Create a list of terms for a given parent, optional with optgroups
 */
function views_handler_filter_terms_by_parent($op, $filterinfo) {
  $options = array();
  foreach (taxonomy_get_children($filterinfo['parent']) as $term) {
    $options[$term->tid] = $term->name;
  }
  if ($filterinfo['group']) {
    foreach (taxonomy_get_children($filterinfo['group']) as $group) {
      foreach (taxonomy_get_children($group->tid) as $term) {
        $options[$group->name][$term->tid] = $term->name;
        unset($options[$term->tid]);
      }
    }
  }
  return $options;
}

Functions

Namesort descending Description
content_taxonomy_views_argument_handler_taxid Perform filtering by an argument (as term id) for field data stored via content taxonomy
content_taxonomy_views_argument_handler_taxletter Perform filtering by an argument (as term name) for field data stored via content taxonomy
content_taxonomy_views_field_handler_group
content_taxonomy_views_field_handler_ungroup
content_taxonomy_views_field_terms_by_parent Display all the terms for a given parent term
content_taxonomy_views_help Implementation of hook_help().
content_taxonomy_views_views_arguments Implementation of hook_views_arguments()
content_taxonomy_views_views_tables
views_handler_filter_terms_by_parent Create a list of terms for a given parent, optional with optgroups