I'm using the imagefield module to place the images in a block with the views module. I created a block with images of recent nodes. I want to make the images in this views block hyperlinks that link to the attached node.

I don't know if this is a views issue or an imagefield issue.. Is there any way to hardcode every image as an hyperlink to its node.

Cheers

View block

  $view = new stdClass();
  $view->name = 'website_latest_thumb';
  $view->description = '';
  $view->access = array (
);
  $view->view_args_php = '';
  $view->page = FALSE;
  $view->page_title = '';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'node';
  $view->url = '';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->block = TRUE;
  $view->block_title = '';
  $view->block_header = '';
  $view->block_header_format = '1';
  $view->block_footer = '';
  $view->block_footer_format = '1';
  $view->block_empty = '';
  $view->block_empty_format = '1';
  $view->block_type = 'table';
  $view->nodes_per_block = '2';
  $view->block_more = FALSE;
  $view->block_use_page_header = FALSE;
  $view->block_use_page_footer = FALSE;
  $view->block_use_page_empty = FALSE;
  $view->sort = array (
    array (
      'tablename' => 'node_data_field_date_year',
      'field' => 'field_date_year_value',
      'sortorder' => 'DESC',
      'options' => '',
    ),
    array (
      'tablename' => 'node_data_field_date_month',
      'field' => 'field_date_month_value',
      'sortorder' => 'DESC',
      'options' => '',
    ),
    array (
      'tablename' => 'node_data_field_date_day',
      'field' => 'field_date_day_value',
      'sortorder' => 'DESC',
      'options' => '',
    ),
  );
  $view->argument = array (
  );
  $view->field = array (
    array (
      'tablename' => 'node_data_field_thumbnail',
      'field' => 'field_thumbnail_fid',
      'label' => '',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'website',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node_data_field_date_year, node_data_field_date_month, node_data_field_date_day, node_data_field_thumbnail, node);
  $views[$view->name] = $view;

Comments

dopry’s picture

Status: Active » Closed (fixed)

You can theme it, or use image cache and one of the formatters that support linking.

moshe weitzman’s picture

It is a bit silly to introduce imagecache just for its link feature. Perhaps we should ship with a 'node link' option in our Views definition.

chuyskywalker’s picture

I agree with the above and would love to have it :D

For such a small chunk of functionality, I'd rather not install yet another module. Especially since this is the only feature I'd want from it.

chrisfab’s picture

Version: 5.x-2.x-dev » 5.x-2.0-rc2

how did you do it?
I tried installing imagecache but don't see how it enables me to create a 'node link' when I create a new view and add an imagefield to "Fields".

It could probably be solved with making a views.inc file in my imagefield module - but I haven't a clue on how to get started or how to do this. I'll gladly give it a go if someone could push me in the right direction?

How else can I make imagefields link to nodes?

apotek’s picture

To fill this out a little bit. Here's how I did it, out of my own ignorance and blind confusion stumbling around in cck, and views code. This is probably wrong, but it works.

In my custom module, which I use just so I can easily override default behavior through the use of hooks, I created the following two functions:

/**
 * Implementation of hook_field_formatter_info().
 * Provides a mechanism through which to override standard cck theming of different field types
 * 
 */
function mymodule_field_formatter_info() {
  $formatters['mymodule_field_as_link'] = array(
  	'label' => t('as link to node'),
  	'field types' => array('image')
  	);
  return $formatters;
}

/**
 * Implementation of hook_field_formatter().
 * Given the formatters defined in mymodule_field_formatter_info, we can now set up actions to do with those fields that have been assigned the formatter arguments we defined there.
 * @param array $field Describes the field type of the item you will be formatting.
 * @param array $item The item you will be formatting
 * @param string $formatter The formatter to use. This will be one of the keys in the $formatters array defined in mymodule_field_formatter_info
 * 
 */
function mymodule_field_formatter($field, $item, $formatter) {
  static $cached_items = array();
  if (empty($cached_items)) {
  	$cache = cache_get('mymodule:files_nodes', 'cache');
  	if (is_object($cache)) {
      $files_nodes = unserialize($cache->data);
      if (is_array($files_nodes)) {
        $cached_items = $files_nodes;
      }
    }
  }
  if ($formatter=='mymodule_field_as_link') {
  	/* if the field is a file (an image), make sure file id is numeric */
    if (isset($item['fid']) && is_numeric($item['fid'])) {
      if (array_key_exists($item['fid'], $cached_items)) {
      	$file = $cached_items[$item['fid']];
      }
      else {
        /** Augh! Ever more reasons Drupal will never scale well. Why can't I get access to node information that has *already* been retrieved fully (and likely more than once already)--just cause I have to work in this russian doll function. So, anyway, here goes another database call. At least we can cache it for the view. */
        $result = db_query('SELECT * FROM {files} WHERE fid = %d', $item['fid']);
        $file = db_fetch_array($result);
        $cached_items[$item['fid']] = $file;
        cache_set('mymodule:files_nodes', 'cache', serialize($cached_items), CACHE_TEMPORARY);
      }
      $output = l(theme('image', $file['filepath'], $item['alt'], $item['title']),'node/'.$file['nid'], array(), NULL, NULL, FALSE, TRUE);
	  return $output;
    }
  }
  return '';
}

Obviously, you'll want to replace "mymodule" with the actual name of the module you use for this sort of thing.

chrisfab’s picture

Title: how to link the image in a view? » improving the imagefield module for views

Hi again. I'm attempting to extend the Imagefield module with additional 'handler' functionalities for Views.
But I'm a bit stuck, as my code renders the wrong images.

I have created a field called 'logo_img' and added it as a field to my Stories.
Thus I reference the table 'content_type_story' as well as the field 'logo_img_fid' in my code.

From my code below I manage to display an image in the browser window with correct a-tag information. But the image is the wrong one, and I'm not sure why.

Can anybody help me out here?
(I would also appreciate comments on how to make my code generic, so others may benefit.)

I have added this code to my local imagefield.module file:

<?php
/**
 * MY ADDITIONAL CODE
 * Implementation of hook_views_tables().
 */
function imagefield_views_tables() {
  $tables['content_type_story'] = array(
    'name' => 'content_type_story',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'nid'
      ),
      'right' => array(
        'field' => 'nid'
      ),
    ),
    'fields' => array(
      'field_logo_img_fid' => array(
        'name' => t('Imagefield: Display Image'),
        'handler' => array(
          'imagefield_views_handler_field_field_logo_img_fid' => t('Image'),
          'imagefield_views_handler_field_field_logo_img_fid_link_node' => t('Image with link to attaching node'),
          'imagefield_views_handler_field_field_logo_img_fid_link_image' => t('Image with link to attached image'),
        ),
/*        'option' => array(
          '#type' => 'select',
          '#options' => 'image_views_handler_filter_image_size',
        ),
*/
        'sortable' => FALSE,
      ),
    ),
  );

  return $tables;
}

/**
 * Views handler for displaying the image.
 */
function imagefield_views_handler_field_field_logo_img_fid($field_info, $field_data, $item, $data) {
  if ($item) {
    $image = node_load($item);
    return imagefield_field_formatter($field_info, $item, NULL);
  }
}

/**
 * Views handler for displaying the image in a link to the node that attaches the image.
 */
function imagefield_views_handler_field_field_logo_img_fid_link_node($field_info, $field_data, $item, $data) {
  if ($item) {
    $image = node_load($item);
	return l(imagefield_field_formatter($field_info, $item, NULL), 'node/'. $data->nid, array(), NULL, NULL, FALSE, TRUE);
  }
}

/**
 * Views handler for displaying the image in a link to the attached image.
 */
function imagefield_views_handler_field_field_logo_img_fid_link_image($field_info, $field_data, $item, $data) {
  if ($item) {
    $image = node_load($item);
	return l(imagefield_field_formatter($field_info, $item, NULL), 'node/'. $item, array(), NULL, NULL, FALSE, TRUE);
  }
}
?>