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;
}