2

I use CiviCRM 4.6.14 with Drupal 7.43. I have defined a Drupal content type with a field that contains a CiviCRM contact using the civicrm_entity module.

When I render this content type, I want to access a few fields from the contact - namely, its name, website, and a Google map link (derived from its address or geocode or whatever's best). In PHP, how can I access these fields from the $content object? If I var_dump($content) I can see the name & geocode, but not a full address (should I piece that together from component fields? seems error prone) and no website field at all.

If I do this by trial and error using var_dump, I'm going to end up with code like

print($content['field_venue']['#items'][0]['entity']->display_name);

which doesn't seem healthy.

4 Answers 4

2

The website is a separate entity from the contact; so is the address data, though some of it is made available (for the primary address only) on the Contact entity.

I know the Address entity is available; I don't think the Website entity is, but that's out of an abundance of caution; it's basically identical to the Phone entity, so you should just be able to edit civicrm_entity.module to patch it in. If you use it and it works, don't forget to submit a patch upstream!

2

So you can configure your Entityreference field to display the rendered entity, and you can choose a view mode. You can configure multiple view modes for CiviCRM Contact entity, and selectively choose which fields to include. I highly recommend enabling Display Suite, and enabling a Display Suite layout for your view mode, as you will get field formatters for CiviCRM properties.

To access subsidiary entities from the contact display, you can add CiviCRM Entity Reference (CER) fields to the Contact entity...this field type is provided by a submodule of the CiviCRM Entity project, and called CiviCRM Entity Reference Field.

You can target addresses, emails, websites, phones etc...by created a CER field for each entity. The CER acts as a "remote reference field". By this I mean that it accesses the subsidary data in the civicrm database directly, and does not store a target_id value in Drupal field tables....

To display say a contact's addresses, on the contact view page (or the rendered contact via an entityreference field on a node view page), you set the CER to display the rendered entity, and like the normal entityreference field type, you can choose a view mode...you can create multiple view modes for the Address entity, configure which fields display, and thus achieve your goal, solely with site building, no code required.

The Website entity is now supported as well in latest -dev (3-26-2017) of CiviCRM Entity, and the process is the same as described for displaying addresses.

1

Using a slightly different approach we have done this many times, if i understand, by building a Drupal View (CiviCRM Contacts based) to pull in the information using Contextual Filters to ensure the relevant data shows on the node. You can see examples if this here.

Pretty much all the data other than the body and map are Civi data that either relate to the contact that was connected to the node via 'CiviCRM reference field'

0

Perhaps you need is CiviFirst's awesome Drupal CiviCRM Session Tokens module, which gives you access to Civi data about the logged in user. That can then be accessed either via the _SESSIONS variable, or through the tokens in blocks or field filters - both of which this module translates for you - , or anywhere else that you can use tokens. I haven't yet included the contact's web address, but it could be easily expanded to cover that.

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.