Answers.Body: <p>So out of the box, CiviCRM Entity enforces that user has a role with 'view event info' permission to see the rendered entity.</p>
  
  <p>In your case, you could probably just grant anonymous users that permission</p>
  
  <p>This can be modified by implementing hook_civicrm_entity_supported_info() in a custom module, where you can change what permissions are necessary to view entities, or you can specifiy a custom access callback handler </p>
  
  <p>See civicrm_entity.api.php</p>
  
  <p>See <a href="https://skvare.com/blog/civicrm-entity-customizing-and-adding-entity-types" rel="nofollow noreferrer">https://skvare.com/blog/civicrm-entity-customizing-and-adding-entity-types</a></p>
  
  <p>Add the functions below to a custom module, replacing YOURMODULE with your module name. </p>
  
  <p>The first implements hook_civicrm_entity_supported_info(). There you set a custom access callback for the event entity. In the callback, check for the view operation, if the event is_public property is not empty, meaning is 1, then check the user has 'view event info' permission. Any other operations and events will enforce 'administer CiviCRM' permission .. you could use another permission, like 'edit all events', or whatever.</p>
  
  <p>UPDATE:</p>
  
  <pre><code>function YOURMODULE_civicrm_entity_supported_info(&amp;$supported_info) {
    $supported_info['civicrm_event']['permissions']['access callback'] = 'YOURMODULE_custom_event_access_callback';
  }
  
  function YOURMODULE_custom_event_access_callback($op, $entity_type, $entity, $account, $permissions) {
    // if $op is view and event is public, check if the user has 'view event info' permissions
    if($op == 'view' &amp;&amp; !empty($entity-&gt;is_public)) {
      // see https://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_access/7.x
      return user_access('view event info', $account);
    }
    // For all other ops and non-public events, check for 'administer CiviCRM' permission
    else {
      return user_access('administer CiviCRM', $account);
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-17T21:30:00.217Z
Answers.Text: updating answer, you could probably just give anonymous users the View Event Info permission
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-18T07:33:53.723Z
Answers.Text: Thanks. It's works.
   
  And how could I limit the view only to public events?
Answers.UserDisplayName: PhilippeS
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-18T23:28:18.450Z
Answers.Text: updated answer with instructions and code example
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-17T18:52:26.547Z
Answers.LastActivityDate: 2018-09-18T23:28:04.873Z
Answers.LastEditDate: 2018-09-18T23:28:04.873Z
Answers.UserDisplayName: jackrabbithanna
Answers.Body: <p>For Drupal 9/10, you need to add an event handler, something like</p>
  <pre><code>function mymodule_entity_type_build(&amp;$entity_info) {
    if (isset($entity_info['civicrm_event'])) {
      $entity_info['civicrm_event']-&gt;setHandlerClass('access', 'Drupal\mymodule\Access\EventAccessControlHandler');
    }
  }
  </code></pre>
  <p>And add a new handler class at <code>src/Access/EventAccessControlHandler.php</code></p>
  <pre><code>&lt;?php
  
  namespace Drupal\mymodule\Access;
  
  use Drupal\civicrm_entity\CivicrmEntityAccessHandler;
  use Drupal\Core\Entity\EntityInterface;
  use Drupal\Core\Session\AccountInterface;
  use Drupal\Core\Access\AccessResult;
  
  class EventAccessControlHandler extends CivicrmEntityAccessHandler {
  
    /**
     * {@inheritdoc}
     */
    protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
      //Custom logic to grant access
  
      return parent::checkAccess($entity, $operation, $account);
    }
  
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-21T11:30:25.880Z
Answers.LastActivityDate: 2024-03-21T11:30:25.880Z
Answers.UserDisplayName: jitendra - fuzion
Body: <p>In drupal a have added a field event to display a event in a web page. When I'm connected I see my Event field. The problem is when a anymous see my page he don't view my rendered entity. </p>
  
  <p>If I change the format of my field to 'label', I can select the option 'Show entity labels regardless of user access' and with this configuration everybody can see my field. Where can I find the same option with the "Rendered Entity" format?</p>
  
  <p>I use CiviCRM 4.6, Drupal 7 with modules CiviCRM Entity, Entity Reference and Display Suite.</p>
  
  <p>Thanks</p>
  
Tags: <drupal><civicrm-entity><entity-reference>
Title: How display event for anonymous user with Rendered Entity
ThreadText: # How display event for anonymous user with Rendered Entity
  
  (Asked by: username_0 on 2018-09-17)
  
  In drupal a have added a field event to display a event in a web page. When I'm connected I see my Event field. The problem is when a anymous see my page he don't view my rendered entity.
  
  If I change the format of my field to 'label', I can select the option 'Show entity labels regardless of user access' and with this configuration everybody can see my field. Where can I find the same option with the "Rendered Entity" format?
  
  I use CiviCRM 4.6, Drupal 7 with modules CiviCRM Entity, Entity Reference and Display Suite.
  
  Thanks
  
  **Tags:** drupal, civicrm-entity, entity-reference
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-09-17. Score: 1 | Accepted answer
  
  So out of the box, CiviCRM Entity enforces that user has a role with 'view event info' permission to see the rendered entity.
  In your case, you could probably just grant anonymous users that permission
  This can be modified by implementing hook_civicrm_entity_supported_info() in a custom module, where you can change what permissions are necessary to view entities, or you can specifiy a custom access callback handler 
  See civicrm_entity.api.php
  See https://skvare.com/blog/civicrm-entity-customizing-and-adding-entity-types
  Add the functions below to a custom module, replacing YOURMODULE with your module name. 
  The first implements hook_civicrm_entity_supported_info(). There you set a custom access callback for the event entity. In the callback, check for the view operation, if the event is_public property is not empty, meaning is 1, then check the user has 'view event info' permission. Any other operations and events will enforce 'administer CiviCRM' permission .. you could use another permission, like 'edit all events', or whatever.
  UPDATE:
  function YOURMODULE_civicrm_entity_supported_info(&$supported_info) {
    $supported_info['civicrm_event']['permissions']['access callback'] = 'YOURMODULE_custom_event_access_callback';
  }
  
  function YOURMODULE_custom_event_access_callback($op, $entity_type, $entity, $account, $permissions) {
    // if $op is view and event is public, check if the user has 'view event info' permissions
    if($op == 'view' && !empty($entity->is_public)) {
      // see https://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_access/7.x
      return user_access('view event info', $account);
    }
    // For all other ops and non-public events, check for 'administer CiviCRM' permission
    else {
      return user_access('administer CiviCRM', $account);
    }
  }
  
  > **Comments:**
  > **username_1**: updating answer, you could probably just give anonymous users the View Event Info permission  
  > **username_0**: Thanks. It's works.   And how could I limit the view only to public events?  
  > **username_1**: updated answer with instructions and code example  
  
  ---
  
  ### Answer by username_2 on 2024-03-21. Score: 0
  
  For Drupal 9/10, you need to add an event handler, something like
  function mymodule_entity_type_build(&$entity_info) {
    if (isset($entity_info['civicrm_event'])) {
      $entity_info['civicrm_event']->setHandlerClass('access', 'Drupal\mymodule\Access\EventAccessControlHandler');
    }
  }
  
  And add a new handler class at src/Access/EventAccessControlHandler.php
  
  
  ---
  
  