Views and hook_views_tables

p. - June 1, 2007 - 01:31

I have read all the documentation for the Views module and have even done some poking around the codebase, but for the life of me, can't figure out how to do this:

I have a module which creates albums of images. I have created an AJAX mechanism for sorting the images within album and a callback that saves the node id and order id of the images once they've been moved. Unfortunately, I can't figure out how to use that order table to order the images upon presenting the view of the album. I've added a hook_views_tables function (see below) and have tried to programatically use $view->sort (see below) to modify the query that gets built as part of views_build_view. Unfortunately, the query that view builds doesn't join the order table with the nodes table.

Why not? What am I missing? If I look in the view tables, nothing is saved there? I assume that's a symptom of my follow.

function acidfree_views_tables() {
    $table = array(
        'name' => 'acidfree_order',
        'join' => array(
            'left' => array(
                'table' => 'node',
                'field' => 'nid'
            ),
            'right' => array(
                'field' => 'nid'
            )
        )
    );
    $tables['acidfree_order'] = $table;
    return $tables;
}

    $view->sort[0] = array(
            'vid' => $view->vid,
            'position' => 1,
            'field' => 'acidfree_order.oid',
            'sortorder' => 'DESC',
            'options' => NULL,
            'tablename' => 'acidfree_order',
            'id' => 'acidfree_order.oid',
        );

Subscribing....for solution

satynos - July 6, 2007 - 19:47

Subscribing....for solution

I know this is an old post

kingandy - March 18, 2008 - 11:45

I know this is an old post but:

You're missing a 'sorts' element in your table array. As well as the 'name' and 'join', you get to describe 'fields', 'sorts', and 'filters' (each providing elements to offer in the respective areas).

This code from the ec_views.module is a good example:

<?php
/**
* Implementation of hook_views_tables()
*/
function ecviews_views_tables() {
 
$tables['ec_product'] = array(
   
'name' => 'ec_product',
   
'join' => array(
     
'left' => array(
       
'table' => 'node',
       
'field' => 'vid'
     
),
     
'right' => array(
       
'field' => 'vid'
     
),
    ),
   
'fields' => array(
     
'sku' => array(
       
'name' => t('Product: SKU'),
       
'sortable' => TRUE,
      ),
     
'price' => array(
       
'name' => t('Product: Price'),
       
'sortable' => TRUE,
      ),
     
'addtocartlink' => array(
       
'name' => t('Product: Add to cart link'),
       
'sortable' => FALSE,
       
'notafield' => TRUE,
       
'handler' => 'views_handler_product_add_to_cart_link',
      ),
    ),
   
'sorts' => array(
     
'sku' => array(
       
'name' => t('Product: SKU'),
      ),
     
'price' => array(
       
'name' => t('Product: Price'),
      ),
    ),
   
'filters' => array(
      
'ptype' => array(
       
'name' => 'Product: Type',
       
'operator' => 'views_handler_operator_andor',
       
'list' => 'ecviews_views_handler_filter_product_type',
       
'list-type' => 'list',
       
'help' => 'Filter by product type',
      ),
     
'is_product' => array(
       
'name' => 'Product: Is Product',
       
'operator' => array('=' => 'Equals'),
       
'list' => 'views_handler_operator_yesno',
       
'list-type' => 'select',
       
'handler' => 'ecviews_views_handler_filter_product',
      ),
     
'pparent' => array(
       
'name' => 'Product: Master Product',
       
'operator' => array('=' => 'Equals'),
       
'list' => array('Product', 'Sub-Product'),
       
'list-type' => 'select',
       
'handler' => 'ecviews_views_handler_filter_product_parent',
       
'help' => t('Filter the product based upon the product being a normal product or a sub-product'),
      ),
    ),
  );
// End ec_product table
[... other tables ...]
  return
$tables;
}
?>

So, if you have a table called 'acidfree_order' and you want to sort by a field in it called 'oid', you'd define the following function:

<?php
function acidfree_views_tables() {
 
$tables['acidfree_order'] = array(
   
'name' => 'acidfree_order',
   
'join' => array(
     
'left' => array(
       
'table' => 'node',
       
'field' => 'nid'
     
),
     
'right' => array(
       
'field' => 'nid'
     
)
    ),
   
'sorts' => array(
     
'oid' => array(
       
'name' => t('Acidfree Order'),
      ),
    );
    return
$tables;
}
?>

Then - fingers crossed - it should be a simple matter of selecting 'Acidfree Order' from the select box in the 'Sort' area of the view in question.

If you wanted to be sure it was sorting in the right order you could also add a 'fields' => array('oid' => array('name'=>'Acidfree Order', 'sortable' => TRUE)), which would allow you to add the oid field to your view (if it is a Table/List view).

--Andy
Developing Drupal websites for Livelink New Media

Addendum

kingandy - March 18, 2008 - 11:55

Oh, also, you may need to clear your Views cache afterwards. If you have MySQL access that's as simple as 'TRUNCATE views_cache;', but I'm not sure what the UI trigger would be for doing that.

--Andy
Developing Drupal websites for Livelink New Media

Clear Views Cache Button

mroswell - September 4, 2008 - 23:02

The Clear Views Cache button is found in the Tools Tab in Views 1:
/admin/build/views/tools

 
 

Drupal is a registered trademark of Dries Buytaert.