2

Scenario is the Content Type has an Entity Reference field set to use Target Type = CiviCRM Event. This currently gives me a radio set for the civievents to choose from.

My concern at this point is about how the field can be displayed. i would like it to provide a link to the registration page of civicrm - i set the Field Display to 'link label to entity reference' but am just getting the event id with no link

2

A more elegant approach would be to use field formatters, in a module, e.g. as below. The advantage with this would be that it works with multiple values and automatically disappears once the registration is closed. You could make it do other things when the registration closes also.

/**
 * Implements hook_field_formatter_info().
 */
function mymodule_field_formatter_info() {
  return array(
    'mymodule_civievent_id_register' => array(
      'label' => t('CiviEvent Registration Button'),
      'field types' => array('entityreference'),
    ),
  );
}

function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {
    case 'mymodule_civievent_id_register':
      civicrm_initialize( );
      foreach ($items as $delta => $item) {
        try{
          $event = civicrm_api3('Event', 'getsingle', array('id' => $item['target_id']));
        }
        catch (CiviCRM_API3_Exception $e) {
          // break;
        }
        if ( time() <= strtotime($event['registration_end_date']) ) {
          $element[$delta] = array(
            '#markup' => l('Register','civicrm/event/register',array(
              'attributes' => array('class' => array('button-like')),
              'query' => array('id' => $item['target_id'],'reset' => 1)
            )),
          );
        }
      }
      break;
  }

  return $element;
}
  • I love it, Alan -- if you have more than one entityreference field would this apply to all of them? – Laryn - CEDC.org Jun 20 '16 at 15:18
  • It would be available to all of them (including those for which it wouldn't actually work, i..e if the entityreference wasn't a reference to a civievent). You'd normally use the manage display and/or views options to choose this formatter for those fields/contexts where it makes sense. – Alan Dixon Jun 21 '16 at 14:39
1

You mentioned that you are using a .tpl file for the event content type -- we've had to do the same on a site and had to massage this field to get what we wanted. The following code is (slightly modified but the basic idea) from the .tpl file specific to the event content type, for example:

if (isset($node->field_civicrm_event['und'][0]['target_id'])) {
   print "<div class='registration_button'><a href='/civicrm/event/register?id=".$node->field_civicrm_event['und'][0]['target_id']."&reset=1'>Register Now</a></div>";
   hide($content['field_civicrm_event']);
}

(Obviously you would need to adjust $node->field_civicrm_event to the actual name of the field in your instance.)

But I agree, the "link label to entity reference" field display could be fixed!

  • Thanks so much for sharing. Will give it a try and report back. – petednz - fuzion Jun 13 '16 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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