I have some issues with the display of images in Table form Views:

  • When using the "Taxonomy Image: Image" option, the table display one line per image/vocabulary.
  • It is not possible to display images from different vocabularies in different columns
    • The best option to solve these issues would be to enable the "Link Alter" module to enable it in these table views. For example add a "As image" to the display options of the field in the Views setup.

      Would it be possible?

Comments

nancydru’s picture

If Views creates them with hook_links it would be. But I don't think that's what Views does. I don't know Views well enough to answer this. I'm not even sure how to set up a test case.

AD-DA’s picture

OK, I did a quick check on the views module. Taxonomy is managed in a sub-module in views_taxonomy.inc.

Below is a "quick and dirty" solution which seems to work for me, but it should be possible to use proper overloading of hooks and handlers to manage this.

First add a new display option in the view definition table:

<?php
function taxonomy_views_tables() {
// [...] code not quoted
        'fields' => array(
          'name' => array(
            'name' => t('Taxonomy: Terms for @voc-name', array('@voc-name' => $voc->name)),
            'sortable' => false,
            'help' => t('This will display all taxonomy terms associated with the node that are members of %voc-name. Note that this causes one extra query per row displayed, and might have a minor performance impact.', array('%voc-name' => $voc->name)),
            'handler' => 'views_handler_field_allterms',
            'vocabulary' => $voc->vid,
            'notafield' => true,
            'option' => array(
               '#type' => 'select',
               '#options' => array(
                 'link' => 'As links',
                 'nolink' => 'Without links',
                 // Display as image option added below
                 'image' => 'As Image'
                ),
            ),
          ),
        ),
// [...] code not quoted
?>

Then change the handler to enable the display of images

<?php
/**
 * Display all the terms for a given vocabulary
 */
function views_handler_field_allterms($fieldinfo, $fielddata, $value, $data) {
  if ($fieldinfo['vocabulary']) {
    $terms = taxonomy_node_get_terms_by_vocabulary($data->nid, $fieldinfo['vocabulary']);
  }
  else {
    $terms = taxonomy_node_get_terms($data->nid);
  }

  if ($fielddata['options'] == 'nolink') {
    foreach ($terms as $term) {
      $links[] = check_plain($term->name);
    }
    $links = !empty($links) ? implode(' | ', $links) : '';
  }
  elseif ($fielddata['options'] == 'image') {
		// $node = new stdClass();
		// load node to get type which is needed  by taxonomy_image_link_alter to check that the type is activated to replace links by images. 
  	$node = node_load($data->nid);
	  // copy terms, but should not be needed
    $node->taxonomy = $terms;
	  // taxonomy_image_link_alter will hook on the taxonomy_link call
    $links = theme('links', taxonomy_link('taxonomy terms', $node));
  }
  else {
    $node = new stdClass();
    $node->taxonomy = $terms;
    $links = theme('links', taxonomy_link('taxonomy terms', $node));
  }
  return $links;
}
?>

The issue comes from the way Views creates the taxonomy links. Probably for performance, it creates a new blank node object where it just copy the taxonomy terms. The node type information does not seems to be available in the data which is sent to the handler. Which means that we need to query the database to get the node type. I just used a node_load, a specific query would be more efficient, but there is probably a better way to manage this.

An other way to solve this issue would be to change the type check on Taxonomy image side:

<?php
function taxonomy_image_link_alter(&$node, &$links) {

  // Is the type one we want to change?
  // If link requested by views, we force the display of the term as image is this display option has been selected in the table setup
  $type_enabled = variable_get('taxonomy_image_link_types', array());
  if ((!$type_enabled[$node->type]) and (!$node->views_display_as_image)){
    return;
  }

?>

This second solution should be more efficient, but it circumvents the Taxonomy Image content type set-up, and override it by the setup of the Views table.

As I am quite new to Drupal, advices on how to manage this properly is most welcome.

nancydru’s picture

Status: Active » Postponed (maintainer needs more info)

Have you submitted a feature request on Views? If so, please post the issue number here.

There may be a way to refactor the testing so that the type checking is still done first.

AD-DA’s picture

Yes I just did: #274378: Enabling Link Alter in Views

and I updated my previous comment with a solution based on changes on Taxonomy Image side.

nancydru’s picture

Can you roll this into a patch, please?

nancydru’s picture

Status: Postponed (maintainer needs more info) » Postponed

When you have time for a patch, please mark this issue active again.

nancydru’s picture

Status: Postponed » Closed (fixed)