I have built a Members Search page using a CiviCRM Contact View in Drupal 7 and I would like to render the Description field of the membership type for the contacts that are returned. This data doesn't appear to be available out of the box so I looked at the CiviCRM Entity module, however that doesn't seem to expose the description. What sort of approach should I take?
3 Answers
I've added a Views relationship to the CiviCRM Entity project that will allow you to do this.
You must also enable the sub-module, CiviCRM Entity Views Extras.
It is in the 7.x-2.x-dev as of Jul 7 2018, and will be included in the next release (2.2)
Once you have the two modules installed, you will be able to add a Views Relationship titled "CiviCRM Member: Membership Type" to your View listing contacts.
Once that Views relationship is in place, all the properties of the membership type table, including the membership type description will be available to include in your view.
Here's export code for an example View that you can import to see for yourself:
$view = new view();
$view->name = 'contact_view_with_membership_type_description';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'civicrm_contact';
$view->human_name = 'Contact View with Membership Type Description';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Contact View with Membership Type Description';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
/* Relationship: CiviCRM Member: Membership Type */
$handler->display->display_options['relationships']['civicrm_membership_type']['id'] = 'civicrm_membership_type';
$handler->display->display_options['relationships']['civicrm_membership_type']['table'] = 'civicrm_membership';
$handler->display->display_options['relationships']['civicrm_membership_type']['field'] = 'civicrm_membership_type';
/* Field: CiviCRM Contacts: Contact ID */
$handler->display->display_options['fields']['id']['id'] = 'id';
$handler->display->display_options['fields']['id']['table'] = 'civicrm_contact';
$handler->display->display_options['fields']['id']['field'] = 'id';
/* Field: CiviCRM Membership Type: Membership Type Description */
$handler->display->display_options['fields']['description']['id'] = 'description';
$handler->display->display_options['fields']['description']['table'] = 'civicrm_membership_type';
$handler->display->display_options['fields']['description']['field'] = 'description';
$handler->display->display_options['fields']['description']['relationship'] = 'civicrm_membership_type';
/* Filter criterion: CiviCRM Member: Membership ID */
$handler->display->display_options['filters']['id']['id'] = 'id';
$handler->display->display_options['filters']['id']['table'] = 'civicrm_membership';
$handler->display->display_options['filters']['id']['field'] = 'id';
$handler->display->display_options['filters']['id']['operator'] = 'not empty';
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'contact-view-with-membership-type-description';
-
Nice. Was there going to be a link or an attachment for the 'example code'? Maybe you overlooked it– petednz - fuzion ♦Commented Jul 8, 2018 at 22:00
-
1
-
Thanks for that @jackrabbithanna. The CiviCRM Entity project was my first stop but I got there too early. If I were to run into this issue again, I'll definitely use it. Commented Jul 24, 2018 at 0:21
Ah yes - many label type fields don't have Views Integration. Since it is a label and presumably not likely to change I would add it to the View myself - For example: as a legend in the Header (or Footer);
-
I assume this needs to be dynamic based on the Membership Type which the member has– petednz - fuzion ♦Commented Apr 15, 2018 at 19:38
-
A Members Search page sounds like a directory in which case the results may be a number of members with different membership types; a legend in the Header (or Footer) would work well; bonus points for colour coding :-) Commented Apr 15, 2018 at 20:16
-
ah. got you. yes explaining all relevant Member Types in header sounds reasonable work around (but having this in Views is the fix). Andrew - sing out if you want to fund some developer support to get this added in to Views. I expect it is ~20-30 mins to fix– petednz - fuzion ♦Commented Apr 16, 2018 at 0:19
-
Thanks folks. Peter is correct in that we do want to have this in views. I was hoping it would be something plug and play but I'm ok with rolling up my sleeves. Peter, I'll Ping you offline if I need to send it your way for development. Commented Apr 25, 2018 at 0:14
-
Hi Andrew - it can already be done in Views - as is - you can create the Header in Views or Footer in Views and if you want it as a Column in Views then you can use a Global txt field in Views with Views PHP. Unless the descriptions are going to be changing frequently - that’s a now/no dev solution Commented Apr 25, 2018 at 12:13
I'm just following up in case someone else decides runs into the same sort of requirements.
The solution was to create a helper module that created a pseudo-calculated field using hook_field_views_data() to declare the field and make it available. Then it used a field handler to run a switch statement to render the appropriate description for the membership level in question.
It required very little code to pull off.