[development] Module with CCK field, View 3, and relationships

Randy Fay randy at randyfay.com
Sat Apr 3 05:19:20 UTC 2010

I'm porting Amazon module to D7. (All the code is in the dev version at

I can't get views relationships to work correctly, and don't know if it's a
views issue or fields issue (probably?)

1. The asin module in the Amazon project provides an ASIN field. This works
fine already. You can add asin elements to a node, it displays right, themes
right, everything.

2. Views is happy to do a node-type view and offers an ASIN as a field, and
displays it, themed correctly.

3. Amazon provides a full amazon_item data type to views, quite a rich set
of data about any amazon item, with prices, pictures, author, and all manner
of detail data all available as views fields. Views is very happy to present
all this data in an amazon_item-type view.

4. BUT: The relationship between a node with an ASIN field and the Amazon
item data is not there. No relationship is offered. So the most popular
feature of D6 Views with Amazon is out of reach: The ability to display an
incredible views list of nodes with lots of rich data about the Amazon items
in its fields.

In D6, asin_field_settings() (hook_field_settings()) had the mapping, I
think, provided in a $op called 'views data'. But hook_field_settings() was
broken up into hook_field_schema() and hook_field_form_settings() and there
doesn't seem to be anything like this any more.

Thanks for any help or ideas!


Below is what the D6 asin_field_settings() had:

function asin_field_settings($op, $field) {
  switch ($op) {
    case 'database columns':
      $columns['asin'] = array('type' => 'varchar', 'length' => 32, 'not
null' => FALSE);
      return $columns;

    case 'views data':
      $data = content_views_field_views_data($field);
      $db_info = content_database_info($field);
      $table_alias = content_views_tablename($field);

      // Filter: Add a 'many to one' filter.
      $copy = $data[$table_alias][$field['field_name'] .'_asin'];
      $copy['title'] = t('@label (!name) - Allowed values', array('@label'
=> $field['widget']['label'], '!name' => $field['field_name']));
      $copy['filter']['handler'] = 'views_handler_filter_many_to_one';
      unset($copy['field'], $copy['argument'], $copy['sort']);
      $data[$table_alias][$field['field_name'] .'_value_many_to_one'] =
      // Argument : swap the handler to the 'many to one' operator.
.'_value']['argument']['handler'] = 'views_handler_argument_many_to_one';

      // Add a relationship for related node.
      $data[$table_alias][$field['field_name'] .'_asin']['relationship'] =
        'base' => 'amazon_item',
        'field' => $db_info['columns']['asin']['column'],
        'handler' => 'views_handler_relationship',
      return $data;

Randy Fay
Drupal Development, troubleshooting, and debugging
randy at randyfay.com
+1  970.462.7450
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20100402/b4f5bfd3/attachment.html 

More information about the development mailing list