1

I am trying to hack a crowdfunding module for Ubercart so that it lets certain vital pieces of information, such as funding goals and the amount already contributed, be displayed on the project pages.

I figured that a good way to do this would be to create a view that would display this information, and then make that the default view for the relevant node-type. Unfortunately, I can't get any fields from the uc_crowdfunding table to appear in the view editor. I even followed this guide should do the trick, as was suggested in this answer.

My added code is as follows:

uc_crowdfunding.module

/**
 * Implements hook_views_api()
 */
function uc_crowdfunding_views_api() {
  return array(
    'api' => '3.0',
    'path' => drupal_get_path('module', 'uc_crowdfunding') . '/views',
  );
}

uc_crowdfunding.views.inc

    /**
     * @file
     * Views hooks for UC crowdfunding projects.
     */

    /**
     * Implements hook_views_data().
     */
    function uc_crowdfunding_views_data() {
      $data['uc_crowdfunding_products'] = array(
        'table' => array(
          'group' => 'Crowdfunding: Crowdfunding products',
          'base' => array(),
        ),
        'cfid' => array(),
        'pfid' => array(),
        'dateline' => array(),
        'target' => array(),
        'type' => array(),
      );
      $data['uc_crowdfunding_products']['table']['base'] = array(
        'field' => 'cfid',
        'title' => t('Crowdfunding product'),
        'help' => t('Data for each crowdfunding product.'),
      );

      // Let's join some tables, eh?
      $data['uc_product_features'] = array(
        'pfid' => array(),
        'nid' => array(),
        'fid' => array(),
        'description' => array(),
      );
      $data['uc_product_features']['join']['uc_crowdfunding_products'] = array(
        'left_field' => 'nid',
        'field' => 'nid',
      );

      // Here are some fields
      $data['uc_crowdfunding_products']['target'] = array(
        'title' => t('Fundraising goal'),
        'help' => t('I guess that this is the amount of money that you\'re supposed to raise, huh?'),
        'field' => array(
          'handler' => 'views_handler_field_numeric',
          'click sortable' => TRUE,
        ),
        'sort' => array(
          'handler' => 'views_handler_sort_group_by_numeric',
        ),
        'filter' => array(
          'handler' => 'views_handler_filter_numeric',
        ),
      );

      return $data;
    }

EDIT: There was also this piece of code, which I had commented out earlier for some reason:

$data['uc_products']['join']['uc_crowdfunding_products'] = array(
  'left_table' => 'uc_product_features',
  'left_field' => 'nid',
  'field' => 'nid',
);
0

You might want to go about this in a different way, like adding a views handler to the module, but I took the question as how to get data from "uc_crowdfunding_products" table into Views. It also seems like the tables mentioned had Node IDs so I made a relationship there. You were pretty close with your code.

Before getting too fancy with relationships and joins, make sure the following basic code works. It adds the ability to list the target field from the products table and some field from the features table with any node associated with them. You'll want to add the node revisions tables if you have revisions on your site. You can also look at the Data project to give you an idea of how to adopt database tables into Drupal.

/**
 * @file
 * Views hooks for UC crowdfunding projects.
 */

/**
 * Implements hook_views_data().
 */
function uc_crowdfunding_views_data() {
  $data['uc_crowdfunding_products'] = array(
    'table' => array(
      'group' => 'Crowdfunding',
      'base' => array(
        'field' => 'nid',
        'title' => t('Crowdfunding Products'),
        'help' => t('Data for each crowdfunding product.'),
        ),
      'join' => array(
        'node' => array(
        'left_field' => 'nid',
        'field' => 'nid',
         ),
      ),
    ),
  );

  $data['uc_crowdfunding_products']['nid'] = array(
    'title' => t('Example content'), 
    'help' => t('Some example content that references a node.'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship', 
      'label' => t('Example node'),
    ),
  );

  $data['uc_crowdfunding_products']['target'] = array(
    'title' => t('Crowdfunding Target'), 
    'help' => t('Just a numeric field.'), 
    'field' => array(
      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  $data['uc_product_features'] = array(
    'table' => array(
      'group' => 'Crowdfunding',
      'base' => array(
        'field' => 'nid',
        'title' => t('Crowdfunding something'),
        'help' => t('Data for each crowdfunding product.'),
        ),
      'join' => array(
        'node' => array(
        'left_field' => 'nid',
        'field' => 'nid',
         ),
      ),
    ),
  );

  $data['uc_product_features']['nid'] = array(
    'title' => t('Example content'), 
    'help' => t('Some example content that references a node.'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship', 
      'label' => t('Example node'),
    ),
  );

  $data['uc_product_features']['some_field'] = array(
    'title' => t('Crowdfunding field'), 
    'help' => t('Just a numeric field.'), 
    'field' => array(
      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  return $data;
}
  • uc_crowdfunding_features actually does not have an 'nid' field, it has 'pfid', 'cfid', and no other ids. I left out code that joins uc_product_features and uc_products on 'nid'. I just added that to the question. Should I put in that ['nid']['relationship'] thing anyway? For the sake of creating a relationship? – jajavoli Jun 9 '14 at 20:58
  • It all depends on what your view consists of. The piece of code you added attempts to join the nid field on each table. What you need is uc_products.nid = uc_product_features.nid and then uc_product_features.pfid = uc_crowdfunding_products.pfid. The key is that the uc_product_features table has both nid and pfid fields to link things however you want. Once again, the Data project I linked might help you join tables and define things. – Alex Finnarn Jun 10 '14 at 0:09
  • The uc_crowdfunding_products table is already claimed by the uc_crowdfunding module. (Remember how it is based on someone else's code.) I don't think the Data module applies, sadly. – jajavoli Jun 10 '14 at 18:14
  • I have, for the moment, decided to forgo the use of Views and have made a custom block in code that directly accesses the database. This is the SQL query that I use: SELECT * FROM {uc_products} p JOIN {uc_product_features} f ON p.nid = f.nid JOIN {uc_crowdfunding_products} c ON f.pfid = c.pfid WHERE p.nid = :nid; – jajavoli Jun 13 '14 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.