My Drupal site draws data from a CRM system that stores info on contacts and companies. In the CRM, the contacts are related to the companies by a common ID. I have set up Drupal to automatically import the contacts and companies as separate content types and keep them up to date. Both the 'contact' and 'company' content types have an ID field, which is used to relate which contacts work for which companies.

I can create a view, with a page that shows an individual company. I am not sure how to attach a view of the contacts that work for the company. I know it would be possible to use node reference and EVA to relate the contacts with the companies manually. Unfortunately when new companies and contacts are added to the CRM the Drupal site needs to be able to create the relationship between the two automatically, probably based on the fact they both share a common value in the ID field.

It would be great to know how other people have managed to create relationships between two content types. I am not sure if there are any modules that can help with this, if I should create a theme template page for the company view page, and hand code some php that queries the database to return the related contacts, or if need to figure out how to build a module to do the above.

Thanks for your help, Paul

  • Progress Update: I have gone down the route of creating a custom module. It creates a block which gets the account ID of the node of the page it is on. It will use Drupal's db_select to query the database and return the appropriate contacts. I am hitting some problems using db_select (here is not the place for a second question), I will report back when I hopefully have this working. May 24, 2013 at 13:55

1 Answer 1


You can do this by using Contextual Filters in Views. Contextual Filters allow you to filter Views results by "Context" (context usually means the currently displayed page or currently logged in user). Assuming you want to display Contacts on a Company page:

  1. Create a new View (admin/structure/views/add) to show content of type Contact. Make sure the Create a block checkbox is checked. If you forget and skip that checkbox, you'll have to add a Block display on the next screen.

  2. Fill out the View so that it looks as you want it to, with the appropriate fields, etc. displayed.

  3. Under Advanced, click to add a Contextual Filter. You're going to be filtering Contacts to display only Contacts which match a certain value for the company ID field. Scroll through the list of fields in the popup window that appears until you find the company ID field. Check the checkbox next to that field and click the Add button.

  4. You need to define the value of the contextual filter. Under WHEN THE FILTER VALUE IS NOT AVAILABLE choose Provide default value. Choose PHP Code for the dropdown Type and enter the following code in the PHP textarea:

    $node = menu_get_object('node');
    if (isset($node) && $node->type == 'COMPANY_CONTENT_TYPE') {
      $company_id = field_get_items('node', $node, 'COMPANY_ID_FIELD_NAME');
      if (!empty($company_id)) {
        return $company_id[0]['value'];

    Replace COMPANY_CONTENT_TYPE and COMPANY_ID_FIELD_NAME with the appropriate machine names for your company content type and the field name used to store the company ID.

    This PHP loads the currently displayed node and loads the value of company ID field from that node. The value returned is used by the contextual filter to display only contacts which have a company ID field which matches this value.

    You can preview the Contacts that will be displayed by manually entering a company ID in the Preview with contextual filters: input while editing the View. The preview should show only Contacts for which the company ID matches that entered value. On a live page view, this value is provided by the PHP above.

  5. Save the View, and go to the Blocks admin page (admin/structure/block). Your new View should appear in the list of available blocks. Drag-drop the block you created into the Content region and save. Optionally configure the block and set it to only display when viewing Company nodes.

At this point you should be able to navigate to any Company node and see the list of contacts as a block, provided by Views. The PHP code entered loads the company ID from the Company node, and filters out all displayed Contacts which don't have a matching value for the company ID field of that Contact.

If you run into any issues or if I missed any steps, please comment below :-p For more information about using contextual filters, I recommend the Taming the Beast: Learn Views with NodeOne video series (parts 19-24 cover contextual filters).

  • Thank you so much thirdender for taking the trouble to write up such a detailed solution. It works perfectly. Thanks also for the link to the NodeOne videos which are excellent. May 31, 2013 at 10:13
  • Cool, glad to hear :-)
    – thirdender
    May 31, 2013 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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