Skip to content

Views Integration

Drupal Views

Views is a powerful Drupal module that allows you to display website content e.g. a list of latest news for the home page. CiviCRM integrates with Drupal Views and allows CiviCRM data to be shown on your website. For instance, if you wanted to create a page called 'Partner Organizations' and display it publicly you could:

  1. Select criteria for what Contact data will be displayed such as:
  2. Organization contacts only
  3. That are tagged 'Partner' in CiviCRM
  4. That have a membership of status 'New' or 'Current'
  5. Choose which data will be shown such as:
  6. Organization name
  7. State/Province
  8. Website
  9. Phone number
  10. Choose to show the data in either table or paragraph format
  11. Allow the public to filter the results themselves by State/Province

This is just a simple example, the possibilities of Drupal Views and CiviCRM are far reaching.

Configuration

If your CiviCRM and Drupal are located within the same database the steps below are not required. These instructions are relevant to Drupal 7 and Drupal 9+ sites where CiviCRM and Drupal are on separate databases located on the same server/container.

  1. Before making any changes, it is strongly advised that you make backups of your mysql databases and settings.php file.
  2. If you have different database users for your CiviCRM and Drupal databases, then you need to grant SELECT access for each database user to the other database: Drupal database user to all the CiviCRM tables, and CiviCRM database user to the Drupal tables. The databases will need to be on the same server or container for this to be possible.
  3. Make sure you are logged into your Drupal site as the administrator.

Drupal 9

  1. In Drupal 9 the civicrm_enity module is also required for Views integrations. Install it with Composer using this command composer require 'drupal/civicrm_entity:^3.0@beta'
  2. Add a $databases in settings.php or settings.local.php similar to:

/*
 * Only use if CiviCRM tables are in a separate database
 * The first array index must be 'civicrm'
 */
$databases['civicrm']['default'] = array (
  'database' => 'DB_NAME',
  'username' => 'DB_USERNAME',
  'password' => 'DB_PASSWORD',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);
You should now be able to create Drupal Views based on CiviCRM data.

Drupal 7

  1. Visit http://example.org/civicrm/admin/setting/uf?reset=1
  2. You should see a page with output that begins with: $databases['default']['default']['prefix']= array(

    Screenshot showing the "Drupal Integration" page, with output of settings to be placed into settings.php

  3. Copy this setting output, including the trailing );

  4. Open the file /sites/default/settings.php for editing from the root directory of the site, typically public_html on most servers.

    Note

    This assumes you have a single site installation. The location of the settings file might be different depending on whether or not you have multiple sites

  5. Go to the bottom of the file and paste in your previously-copied settings output here. If the settings.php file already includes an earlier version of the output please remove the earlier version before pasting the new output.

    Screenshot demonstrating the output of database settings

  6. You may need to indicate the default prefix. If you see an error like the one below:

    PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'kckruppc_every.semaphore' doesn't exist: SELECT ...

    You need to include an additional line, 'default' => '<drupaltableprefix>', in your settings.php just after $database ['default']['default']['prefix']= array(. You can find more information on this error in this forum post.

    Screenshot demonstrating where to add a table prefix in your settings

  7. Save the file settings.php. You might want to clear your site cache at this point.

You should now be able to create Drupal Views based on CiviCRM data.

!!! note If you add a new set of custom fields to CiviCRM you will need to re-do these steps again. So any new fields in the field set can be accessed by Drupal Views.

Creating Views Using CiviCRM Data

Views are located in the Structure section of the Drupal administrative menu. When you create a View, give it a name and select what kind of data you wish to show.

Generally speaking if your view is focused on Contacts (which most are) you will select Show: CiviCRM Contacts. If you wish to display details about other CiviCRM data such as events, relationships, contributions or activities there are additional options that might provide more data fields for those types.

Screenshot demonstrating the "Add new view" page

After the View is created, edit the fields, filters, display and other configurations to show the data exactly how you'd prefer. Views does take some experimentation and/or training to get it right. Feel free to ask questions on Stack Exchange or Mattermost — or hire a consultant if you become stuck.

Screenshot demonstrating the "Edit view" page

This is a sample configuration using Views 3 in a Drupal 7 environment. Your View will probably be slightly different, but this gives you some examples to get started with.

Other Things Views and CiviCRM Can Do

  • Lists of upcoming events
  • Building reports on contributions, activities or event registrants
  • Honor roll listing of recent donors
  • Staff, board of directors or committee lists
  • A list of current members
  • ...and more!