Views Integration Example

rhubarb-media - November 5, 2009 - 19:01
Project:Ubercart Ajax Attribute Calculations
Version:6.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:closed
Description

I found a way to add Views support to the 6.x-1.x-dev branch.

STEP 1

On line 113 of the "uc_aac.module" file, the Jquery selector is looking for an element with the node's id:

$("#node-'. $nid .' select")

And on the project page you mention that product node div's must have the following identifier, so that Jquery can locate the correct information:

id="node-<?php print $node->nid; ?>"

This works great on the product node pages, but Views does not output this identifier for each row, so the Javascript fails. To solve this problem, I templated my View to wrap each row in a div with the proper identifier (see below). This allows Jquery to select the form elements in the product's "add to cart" form.

STEP 2

On lines 215 and 216 of the "uc_aac.module" file, the Jquery selectors are looking for elements with "uc-price-sell" and "uc-price-display" classes. Once again, Views does not output these classes for each row, so I had to add them to my template file (see below). This allows Jquery to select and update the product's price on the page.

It doesn't look like Views allows you to output the "display price", so I ignored this and focused exclusively on the "sell price".

THE TEMPLATE FILE

<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
* @file views-view-fields.tpl.php
* Default simple view template to all the fields as a row.
*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
*   - $field->content: The output of the field.
*   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
*   - $field->class: The safe class id to use.
*   - $field->handler: The Views field handler object controlling this field. Do not use
*     var_export to dump this object, as it can't handle the recursion.
*   - $field->inline: Whether or not the field should be inline.
*   - $field->inline_html: either div or span based on the above flag.
*   - $field->separator: an optional separator that may appear before a field.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
*/
?>


<div id="node-<?php print $row->nid; ?>">

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>
      <?php
     
// $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
     
?>

      <<?php print $field->element_type; ?> class="field-content">
    <?php
         
// Wrap the "sell-price" field output in an additional span
         
if ($field->class == 'sell-price') {
            print
'<span class="uc-price-sell">'.$field->content.'</span>';
          } else {
            print
$field->content;
          }
       
?>

      </<?php print $field->element_type; ?>>
  </<?php print $field->inline_html;?>>
<?php endforeach; ?>

</div>

Thanks for creating and maintaining this module! We're using it on multiple sites.

#1

AntoineSolutions - November 10, 2009 - 22:43
Status:active» closed

Hi rhubarb-media,

I appreciate your interest in this module. Thank you for the views integration documentation, but I am unable to find any request for a particular feature in your post. I would like to encourage you to contribute your documentation to Drupal, please see http://drupal.org/node/14279. The one question I was able to formulate out of your post was how to output the display price in views. The display price is simply the sell price with the "uc-price-display" class.

I am going to close this issue. If I failed to see a feature request that you were trying to bring to our attention, please repost with a little more clarity on what exactly it is you would like the module to do.

Cheers,

Antoine

#2

rhubarb-media - November 11, 2009 - 23:51
Title:Views Integration» Views Integration Example
Category:feature request» task

Sorry Antoine. I was originally going to ask for Views integration in the Ubercart Ajax Attribute Calculations module, but I soon realized that I could accomplish this by templating my Views. This post started out as a feature request and turned into a tutorial. Hopefully someone will find it useful.

I've changed this issue's category to "task" instead of "feature request" (it wouldn't let me choose "none").

 
 

Drupal is a registered trademark of Dries Buytaert.