Answers.Body: <p>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.</p>
  
  <pre><code>/**
   * Implements hook_field_formatter_info().
   */
  function mymodule_field_formatter_info() {
    return array(
      'mymodule_civievent_id_register' =&gt; array(
        'label' =&gt; t('CiviEvent Registration Button'),
        'field types' =&gt; 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 =&gt; $item) {
          try{
            $event = civicrm_api3('Event', 'getsingle', array('id' =&gt; $item['target_id']));
          }
          catch (CiviCRM_API3_Exception $e) {
            // break;
          }
          if ( time() &lt;= strtotime($event['registration_end_date']) ) {
            $element[$delta] = array(
              '#markup' =&gt; l('Register','civicrm/event/register',array(
                'attributes' =&gt; array('class' =&gt; array('button-like')),
                'query' =&gt; array('id' =&gt; $item['target_id'],'reset' =&gt; 1)
              )),
            );
          }
        }
        break;
    }
  
    return $element;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-20T15:18:28.163Z
Answers.Text: I love it, Alan -- if you have more than one entityreference field would this apply to all of them?
Answers.UserDisplayName: Laryn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-21T14:39:41.993Z
Answers.Text: 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.
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-14T17:25:45.200Z
Answers.LastActivityDate: 2016-06-14T17:25:45.200Z
Answers.UserDisplayName: Alan Dixon
Answers.Body: <p><a href="https://civicrm.stackexchange.com/questions/4631/sort-issue-with-civicrm-entity-reference-on-a-drupal-node?noredirect=1#comment13927_4631">You mentioned</a> 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:</p>
  
  <pre><code>if (isset($node-&gt;field_civicrm_event['und'][0]['target_id'])) {
     print "&lt;div class='registration_button'&gt;&lt;a href='/civicrm/event/register?id=".$node-&gt;field_civicrm_event['und'][0]['target_id']."&amp;reset=1'&gt;Register Now&lt;/a&gt;&lt;/div&gt;";
     hide($content['field_civicrm_event']);
  }
  </code></pre>
  
  <p>(Obviously you would need to adjust <code>$node-&gt;field_civicrm_event</code> to the actual name of the field in your instance.)</p>
  
  <p>But I agree, the "link label to entity reference" field display could be fixed!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-13T20:16:38.433Z
Answers.Text: Thanks so much for sharing. Will give it a try and report back.
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-13T16:14:19.120Z
Answers.LastActivityDate: 2016-06-13T16:14:19.120Z
Answers.LastEditDate: 2017-04-13T12:53:13.167Z
Answers.UserDisplayName: Laryn
Body: <p>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.</p>
  
  <p>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</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-12T05:01:21.627Z
Comments.Text: Perhaps Drupal module EVA (https://www.drupal.org/project/eva) and use the event id in a link generated by the attached view?
Comments.UserDisplayName: Allen Hutchison
Tags: <civievent><drupal><civicrm-entity>
Title: How to link to the CiviEvent registration page from a node field using entity reference
ThreadText: # How to link to the CiviEvent registration page from a node field using entity reference
  
  (Asked by: username_0 on 2016-06-12)
  
  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
  
  **Tags:** civievent, drupal, civicrm-entity
  
  > **Comments:**
  > **username_1**: Perhaps Drupal module EVA (https://www.drupal.org/project/eva) and use the event id in a link generated by the attached view?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2016-06-14. Score: 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;
  }
  
  ```
  
  > **Comments:**
  > **username_3**: I love it, username_2 -- if you have more than one entityreference field would this apply to all of them?  
  > **username_2**: 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.  
  
  ---
  
  ### Answer by username_3 on 2016-06-13. Score: 1
  
  [You mentioned](https://civicrm.stackexchange.com/questions/4631/sort-issue-with-civicrm-entity-reference-on-a-drupal-node?noredirect=1#comment13927_4631) 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!
  
  > **Comments:**
  > **username_0**: Thanks so much for sharing. Will give it a try and report back.  
  
  ---
  
  