Friday, October 28, 2011 - 08:45
Written by

Recently we spent some time cleaning up the code base for Drupal's Views module in CiviCRM. This will be reflected in the 4.1 release of CiviCRM, at this point this was all written for Drupal 7, though should be portable to Drupal 6. There were a couple new features thrown in but much of this will focus on making the code base a little more efficient and easier to read.

The two major tasks were:

Break up the file into separate files for each component. This is referring to the file that holds the Views related hooks (ie hook_views_data() and others) to allow the CiviCRM and Views modules to work together. Over the past couple years this file has grown to an unweildy 7,000+ lines. The lions share of the 7,000+ lines of code was the call to hook_views_data(). That function has been broken into 7 files, one for each CiviCRM Component (Core, Contribute (includes Pledge), Membership, Campaign, Event, Grant and Mail). Additionally each Component's fields are only available to Views if the Component is active. All of these Component files live in the components directory under the main views directory.

Create generic Pseudo Constant filter and field handlers. Pseudo Constants are sort of an inside the ballpark concept for the set of options lists that ship with CiviCRM whose choices are relatively static (examples would be State/Province, Contact Type or Contribution Type). CiviCRM shipped 2 files (called handlers) for each Pseudo Constant that was exposed in Views. These files contained exactly the same 25 or so lines with the exception of 2 lines. These two lines varied only by the name of the function used in CiviCRM to return the options for the relevant Pseudo Constant (ie CRM_Campaign_PseudoConstant::campaignType( ) ). A pair of generic handlers files was written one for Fields and other for Filters ( civicrm_handler_field_pseudo_constant and civicrm_handler_filter_pseudo_constant respectively) in Views, which can be passed a pair of arguments in the hook_views data() call. This is all laid out on the forums here,21116.msg88509.html#msg88509. The end result was 51 files no longer need to ship with CiviCRM and exposing new Pseudo Constant fields to CiviCRM is significantly easier.

Both of these should make it easier to maintain the Views integration and for developers to expose fields to Views. For the future the plans are to move to more stable integration by creating unit test for the views integration and documenting how to add fields of your own.

For those of you who don't care about code we are looking at one fun new feature which is to allow Views to become CiviCRM Dashboard Dashlets, more in a future blog post.

Filed under


Excellent news--good work. Thank you. :)

optimizing for drupal 7.. GOOD STUFF! Keep up the excellent work.