1

I have two content types, "Stores" and "Offers." In the "Offers" content type, there is a CCK node reference field type that references the "Stores" nodes. So essentially, I create a store and if the store has an offer, I create the "Offer" node and select the store with which it is associated.

I have a view (a default glossary view) that displays a list of stores. I would like to display the node titles of any offers that are associated with it but I can't figure out how to do it.

I have tried to add a relationship on the node reference field then add a field that uses the relationship but the result just appears blank.

1

In your view style template you can access the node reference value, load the related node and then do whatever you need with that. You don't need a relationship for this approach.

|improve this answer|||||
  • That is a possibility but I don't want to have to do this through a template as it is both more memory intensive and less flexible. – Jamie Hollern May 25 '11 at 17:10
0

I finally went with something similar to Stefan's answer. I created a module using hook_views_pre_render that checks the correct table against the node id to see if there are any related nodes. The code is shown below.

<?php

/**
 * Implementation of hook_views_pre_render()
 */
function gretna_views_pre_render(&$view) {
    // Select the right view
    if ($view->name == 'stores_list') {
        // Check how many results there are
        $count = count($view->result);
        // For each result (reason we don't use foreach is every result is duplicated twice)
        for($i = 0; $i < $count; $i++) {
            // Get the nid for this row
            $nid = $view->result[$i]->nid;
            // Check to see if the node has any related offers
            $result = db_query("SELECT nid FROM {content_type_store_offers} WHERE field_offer_store_nid = %d", $nid);
            $result = db_fetch_array($result);
            // If not false, the row has offers
            if ($result !== FALSE) {
                $view->result[$i]->users_uid = "Yes";
            }
            // Else it doesn't
            else {
                $view->result[$i]->users_uid = "No";
            }
        }
    }
}
|improve this answer|||||

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.